Map()

Advertisement

With the help of map() array method we’ll modify and create new array from existing array.

Suppose we have array:

let a = [
    {
        name: 'Mahesh',
        age: 23,
    }, {
        name: 'Jyoti',
        age: 21,
    },
];

The array is stored as:

0: {name: 'Mahesh', age: 23}
1: {name: 'Jyoti', age: 21}

Suppose we want to create a new array by adding the empty address key.

Then, We’ll use below code:

let b = a.map( item => {
    item.address = '';
    return item;
});

Then, The variable b store the value as:

// Output:
0: {name: 'Mahesh', age: 23, address: ''}
1: {name: 'Jyoti', age: 21, address: ''}

Here, The new array is created with new key address with empty value.

Leave a Reply