I am trying to add the new property to the array of objects but while iterating, I am losing the existing values.
Is there any way, I can modify just the object's property and also do a null check?
I tried following.
const data = [
{
testid: 1,
items: {
id: 1,
desc: 'test'
}
},
{
testid: 2,
items: {
id: 2,
desc: 'test1'
}
},
{
testid: 3
}
];
let result = data.map(({ items }) => ({
...items,
newval: items?.id + ' - ' + items?.desc
}));
console.log(result);
//Expected Output
const newData = [
{
testid: 1,
items: {
id: 1,
desc: 'test',
newval: '1 -test'
}
},
{
testid: 2,
items: {
id: 2,
desc: 'test1',
newval : '2 -test1'
}
},
{
testid: 3
}
];