Here is a simplified version of my current setup.
When running this code you'll notice that modifying the changes array also manipulates the original referrals array. Is this because I'm pushing to the changes array inside a map function? How can I modify the changes array without modifying the referrals array?
var referrals = [
{
id: 1,
name: 'John',
change: true
},
{
id: 2,
name: 'Sally',
change: false
},
{
id: 3,
name: 'Kim',
change: true
}
];
var changes = [];
var process = referrals.map(function(referral) {
return new Promise(function(resolve, reject) {
if (referral.change) {
changes.push(referral);
}
resolve();
});
});
Promise.all(process).then(function() {
console.log('referrals before:', referrals);
changes = changes.map(function(change) {
change.change_id = change.id;
delete change.id;
return change;
});
console.log('changes:', changes);
console.log('referrals after:', referrals);
});
referralsArray. You're changing the Objects it contains. And you'll have the same results without using Promises. When you push your Objects into thechangesArray, you're actually pushing references to the Objects that are in thereferralsArray. Not copies of them, which is what you seem to think. So then, when you change a property of these Objects, these differences will show everywhere these Objects are referenced. To solve this, look up "JS clone Object"