My question is related to Add key value pair to all objects in array
However, the solutions don't work when I try to assign an object instead of a string, int etc.
I tried to create the new key inside map function, but it only works with non-object variables.
This works
arrObjects.map(item => item.newKey = 'xxx')
This doesn't
var obj = { a: 'a', b: 'b', c: 'c' }
arrObjects.map(item => item.newKey = obj)
Output:
var arrOfObj = [{
name: 'eve'
}, {
name: 'john'
}, {
name: 'jane'
}];
var obj = {
a: 'a',
b: 'b',
c: 'c'
}
arrOfObj.map(item => item.newKey = obj);
console.log(arrOfObj);
.as-console-wrapper { max-height: 100% !important; top: 0; }