I have an Array (oldArray) of 120 objects. I want to make another Array (newArray) whose first element is the first element of oldArray. Seems simple enough, except that my outputs are not as expected.
var obj = oldArray[0];
newArray[0] = obj;
console.log(obj);
console.log(newArray);
console.log(newArray[0]);
console.log(oldArray);
console.log(oldArray[0]);
obj
, newArray[0]
, and oldArray[0]
all produce the same result in my console -- the single object that I want to work with.
newArray
however shows all objects of oldArray
, not just the one that I thought obj contained. newArray.length == 1
. Console displays: [Object]
oldArray
is my original array. oldArray.length == 120
. Console displays [Object, Object, ...]
I have tried many things and did not expect to get hung up on this. I thought it would have been newArray.push(oldArray[0])
or maybe newArray[0] = oldArray.splice(0,1)
but everything I attempt seems to be creating the same issue.
Is there some kind of special trick for working with arrays of Objects?
Thanks!
newArray
andoldArray
declared as an Array? – jfriend00 Nov 19 '13 at 3:27