I am trying to loop through an Array of Objects, modify each Object, and push the result into a new Array.
The Objects in the array have different values. When I examine the resulting Array all of the Objects all have the same value. They all reflect the last Object in the original Array
For Example:
const a = { choo: 'choo' }
const b = [ { foo: 'bar' }, { foo: 'baz' }, { foo: 'foo' } ]
let c = [];
b.forEach((item) => {
c.push(Object.assign(a, {...item} ));
});
The result of c:
[
{ foo: 'foo', choo: 'choo' },
{ foo: 'foo', choo: 'choo' },
{ foo: 'foo', choo: 'choo' }
]
I assume this is some sort of scope issue but I'm not sure how to fix it.
Expected results would be
[
{ foo: 'bar', choo: 'choo' },
{ foo: 'baz', choo: 'choo' },
{ foo: 'foo', choo: 'choo' }
]