EN
TypeScript - array map method example
4 points
In TypeScript it is posslbe to map array in following way.
This section shows how to make example conversion of array items.
xxxxxxxxxx
1
let inputArray = [
2
{
3
id : 1,
4
name : 'John',
5
email : 'john@gmail.com',
6
password : 'john-password',
7
age : 43
8
},
9
{
10
id : 2,
11
name : 'Kate',
12
email : 'kate@gmail.com',
13
password : 'kate-password',
14
age : 54
15
},
16
{
17
id : 3,
18
name : 'Diego',
19
email : 'diego@gmail.com',
20
password : 'diego-password',
21
age : 21
22
},
23
];
24
25
let outputArray = inputArray.map((oldItem : any) : any =>
26
{
27
let newItem = {
28
name : oldItem.name,
29
age : oldItem.age
30
};
31
32
return newItem;
33
});
34
35
console.log(outputArray);
Output:
xxxxxxxxxx
1
[
2
{ name: 'John', age: 43 },
3
{ name: 'Kate', age: 54 },
4
{ name: 'Diego', age: 21 }
5
]
Note:
Array.prototype.map
method is usefull to simplyfy arrays.