Method to add an additional property to each object in an array.
consider the array as
const data = [
{ id: 1, name: 'John', address: '123 Main St' },
{ id: 2, name: 'Jane', address: '456 Elm St' },
{ id: 3, name: 'Bob', address: '789 Oak St' }
];
we need to add additional property to each object inside the array
Additional Property country: USA is added to the array as follows:
const newData = data.map(obj => {
?return { ...obj, country: 'USA' };
});
console.log(newData);
The Output :
[
?{ id: 1, name: 'John', address: '123 Main St', country: 'USA' },
?{ id: 2, name: 'Jane', address: '456 Elm St', country: 'USA' },
?{ id: 3, name: 'Bob', address: '789 Oak St', country: 'USA' }
]