Skip to content Skip to sidebar Skip to footer

Unreferenced Array Not So Unreferenced After "slice"?

Note: this isn't one of the millions dups of the common array copy 'problem' where using arr.slice(0) fixes this 'problem' That said, I want to understand why I am getting this une

Solution 1:

Everything is working as expected

indexOf searches by reference and not value, therefore if two objects do not === each other, indexOf will not find them

In your case:

var oldArr = [[1,2],[3,4]];
var find = oldArr[1];

var newArr = oldArr.slice(0);

find points to the array [3,4]; so newArray contains 2 objects (the two arrays). If you want to see what is really going on try doing find.push(5)

Post a Comment for "Unreferenced Array Not So Unreferenced After "slice"?"