Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
1  
it looks fine jsfiddle.net/arunpjohny/YK5aW/1 –  Arun P Johny Nov 19 '13 at 2:54
1  
Are both newArray and oldArray declared as an Array? –  jfriend00 Nov 19 '13 at 3:27
    
Ok, turns out that some stuff I was doing with newArray later on was messing it up, but for some reason that console.log was showing a LATER version of newArray (I had no other console.logs anywhere else). Thing works perfectly. Thanks, all. –  Chris Ramsey Nov 19 '13 at 23:36

1 Answer 1

up vote 2 down vote accepted

I've tried replicating your issue and these are my results:

var oldArray = ['a','b','c','d'];
var newArray = [];

var obj = oldArray[0]; // store the first value in a new variable
newArray[0] = obj; // push the variable's value to the first index of the new array

console.log(obj);
    // 'a'
console.log(newArray);
    // ['a']
console.log(newArray[0]);
    // 'a' (the same as obj)
console.log(oldArray);
    // ["a", "b", "c", "d"]
console.log(oldArray[0]);
    // 'a'

Based on the scope of the script and the data in oldArray, these is correct behaviour. Either your test case isn't reduced properly or the problem isn't reflected by your question.

Since I was testing with strings and not objects, there may be some differing behaviour in your specific use case, but giving some sample data about the contents of oldArray would greatly help your cause.

share|improve this answer
    
I am certain that there is something more going on with my code. Both are arrays but when I test typeof, I get 'object'. Is that normal? –  Chris Ramsey Nov 19 '13 at 18:03
1  
In JS, both objects and arrays have a typeof "object". The difference between them can be assessed by doing item.length. Objects have an undefined length, and arrays will be 0 or larger. –  Bryce Hanscomb Nov 20 '13 at 0:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.