Updating An Array Of Objects Without Mutation
If I have an object: [ { 'value':'d1', 'label':'bathstore.com', 'selected':true }, { 'value':'d2',
Solution 1:
You can iterate over array with map
and inside each callback use spread syntax to create new objects with updated property:
let data = [
{
"value":"d1",
"label":"bathstore.com",
"selected":true
},
{
"value":"d2",
"label":"superdrug.com",
"selected":true
},
{
"value":"d3",
"label":"papajohns.com",
"selected":true
}
];
let newData = data.map((item) => {
return {...item, selected: false};
});
console.log(newData);
Post a Comment for "Updating An Array Of Objects Without Mutation"