. . .
var sample = [{ a: 1, b: 1, c: 1}, { a: 1, b: 1, c: 1}, { a: 1, b: 1, c: 1}];
var groupsOfItems = [];
var Item = function(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
for (let i = 0; i < 10; i++) {
var item = {};
item.sample = _.map(sample, function(item) {
return new Item(item.a, item.b, item.c)
});
groupsOfItems.push(item);
}
groupsOfItems[0].sample[0].a = 10
console.log(groupsOfItems[0].sample[0].a, groupsOfItems[1].sample[0].a, groupsOfItems[2].sample[0].a);
Thus, you assign them the entire container for your modifications, and the cloning problem goes away.
source
share