EN
TypeScript - array map method example
4
points
In TypeScript it is posslbe to map array in following way.
1. Array
map
method example
This section shows how to make example conversion of array items.
let inputArray = [
{
id : 1,
name : 'John',
email : 'john@gmail.com',
password : 'john-password',
age : 43
},
{
id : 2,
name : 'Kate',
email : 'kate@gmail.com',
password : 'kate-password',
age : 54
},
{
id : 3,
name : 'Diego',
email : 'diego@gmail.com',
password : 'diego-password',
age : 21
},
];
let outputArray = inputArray.map((oldItem : any) : any =>
{
let newItem = {
name : oldItem.name,
age : oldItem.age
};
return newItem;
});
console.log(outputArray);
Output:
[
{ name: 'John', age: 43 },
{ name: 'Kate', age: 54 },
{ name: 'Diego', age: 21 }
]
Note:
Array.prototype.map
method is usefull to simplyfy arrays.