I have an array that contains a set of objects And in these objects I want to change the value of a property :
var array=[{a:1, b:false}, {a:2, b:true}, {a:3, b:false}]
I want the propriety b to be true everywhere
var array=[{a:1, b:true}, {a:2, b:true}, {a:3, b:true}]
How do I do this?
Just use a loop with map:
const newArr = array.map((item) => ({ ...item, b: true }));
you can use two possible approaches:
forEach as follow:yourArray.forEach(item=>item.b = true);
map as follow:const updatedArray = temp.map(item=>{...item, b:true});
There are 2 ways you can achieve the result:
forEach:var array=[{a:1, b:false}, {a:2, b:true}, {a:3, b:false}]
array.forEach((obj) => {
return obj.b = true
})
console.log(array)
map:var array=[{a:1, b:false}, {a:2, b:true}, {a:3, b:false}];
const result = array.map((item) => ({ ...item, b: true }));
console.log(result);