I am trying to merge each object in an array of objects (that are all object literals) with a "prototype" that is an also an object literal:
var objArray = [{
first: 123,
second: "asd",
third: function () {
alert("hello");
},
items: {
color: "brown",
number: 10
}
}, {
first: 765,
second: "lkj",
third: function () {
alert("goodbye");
},
items: {
color: "yellow",
number: 999
}
}];
var obj2 = {
first: 143,
items: {
color: "blue",
number: false
}
};
var combinedObjArray = [];
var i;
for (i = 0; i < objArray.length; i += 1) {
var newObj = $.extend(true, obj2, objArray[i]);
combinedObjArray.push(newObj);
}
alert(JSON.stringify(combinedObjArray));
My issue is that "newObj" just creates a pointer, instead of creating unique objects (I think):
This line…
… should actually be written as …
… otherwise you’ll augment an
obj2each time it’s called: