The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Modify Values in an Array with Map

Modify Values in an Array with Map

9:26
Map is one of the most useful array methods. It creates a new array with the exact same number of items as the source and can be used to modify values, change their type, add fields to objects, simplify objects etc. In this lesson we focus on practical use-cases for map & see it in conjunction with other array methods.
Watch this lesson now
Avatar
egghead.io

Map is one of the most useful array methods. It creates a new array with the exact same number of items as the source and can be used to modify values, change their type, add fields to objects, simplify objects etc. In this lesson we focus on practical use-cases for map & see it in conjunction with other array methods.

Avatar
Vamshi

In the example here, we wanted to add lastname property to each object.
const items = [
{
firstname: 'Shane',
lastname: 'Osbourne'
},
{
firstname: 'Sally',
lastname: 'Osbourne'
},
{
firstname: 'Ben',
lastname: 'Barker'
}
];

Now what if I dont know how my objects look like from the API call, and lets say each object is not too identical, for example,
const items = [
{
firstname: 'Shane',
lastname: 'Osbourne',
income: '200000'
},
{
firstname: 'Sally',
lastname: 'Osbourne',
kids: 2
},
{
firstname: 'Ben',
lastname: 'Barker'
}
];

Is there a way we can still add a property to the object, like
const items = [
{
firstname: 'Shane',
lastname: 'Osbourne',
income: '200000',
status: good
},
{
firstname: 'Sally',
lastname: 'Osbourne',
kids: 2,
status: good
},
{
firstname: 'Ben',
lastname: 'Barker',
status: good
}
];

Like that using map? Thank you

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?