Reputation: 409
Given this fiddle, does anyone have a suggestion as to how I might update the indices of array1? Or more to the point, any idea how to make the indices of array2 references to indices of array1?
var array1 = [
{num:"one"},
{num:"two"},
{num:"three"}
];
var array2 = [];
var i = array1.length;
while(i--){
if(i!=1)array2.push(array1[i]);
}
array2[0].num = "one updated";
console.log(array2);
console.log(array1);
Obviously, in this codeblock, array1[0] is not updated.
Upvotes: 0
Views: 54
Reputation: 11859
since your array is set of objects try like this:
var array1 = [
{num:"one"},
{num:"two"},
{num:"three"}
];
var array2 = [];
for(x in array1){
array2.push(array1[x]);
}
array2[0].num = "one updated";
console.log(array2);//output [Object { num="one updated"}, Object { num="two"}, Object { num="three"}]
console.log(array1);// output [Object { num="one updated"}, Object { num="two"}, Object { num="three"}]
Upvotes: 1