I have below array object –
[
{
index:0,
value:"Yes",
},
{
index:1,
value:"No",
},
{
index:2,
value:"No",
},
{
index:3,
value:"Yes",
},
]
I want to update value of each element in array with "NA"
I tried doing –
obj.map(x=>{
x.value="NA"
});
This is not working.
>Solution :
You could use two ways
forEach, which would mutate the existing array
const obj = [
{
index: 0,
value: "Yes",
},
{
index: 1,
value: "No",
},
{
index: 2,
value: "No",
},
{
index: 3,
value: "Yes",
},
]
obj.forEach(x => {
x.value = "NA"
})
console.log(obj)
map, which will return the new array
const obj = [
{
index: 0,
value: "Yes",
},
{
index: 1,
value: "No",
},
{
index: 2,
value: "No",
},
{
index: 3,
value: "Yes",
},
]
const newObj = obj.map(x => ({
...x,
value: "NA",
}))
console.log(newObj)