Languages
[Edit]
EN

TypeScript - array map method example

4 points
Created by:
maciek211
335

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.

TypeScript

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join