Languages
[Edit]
EN

TypeScript - array map method example

4 points
Created by:
Kate_C
2854

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.

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.