Here is a snippet of code:
let data = [1, 2];
let newBody = {};
let newArray = data.reduce((acc, current) => {
newBody.doc = current;
acc.push(newBody);
return acc;
}, []);
The resulting array is:
newArray = [ { doc: 2 }, { doc: 2 } ]
If I redeclare the empty newBody inside the loop, it works correctly. However, if I declare it outside the loop, the value of the last array element is applied to all other elements and I'm unsure why.