0

I have an array of objects. I am attempting to locate the index of an object inside this array, however it always returns -1 unless I "cheat". I cannot see the difference here and it console.logs the exact same output both ways. It only works one way... Is this a bug?

var arr = [{ un: 'test', uid: 1 }];
console.log("array",arr,typeof arr);

var obj = { un: 'test', uid: 1};
console.log("obj",obj);

console.log(arr[0],typeof arr[0]);
console.log("indexOf",arr.indexOf(obj));

arr.push(obj);
console.log(arr);
console.log("indexOf",arr.indexOf(obj));

The Output is this:

array [ { un: 'test', uid: 1 } ] object
obj { un: 'test', uid: 1 }
{ un: 'test', uid: 1 } 'object'
indexOf -1
[ { un: 'test', uid: 1 }, { un: 'test', uid: 1 } ]
indexOf 1

For the life of me, I cannot see the difference here. Am I missing something simple here? It's possible... considering the hours of coding, simple things get overlooked.

1 Answer 1

1

In javascript, Objects are always references to the object itself (pointers to memory address where the object is stored), so doing

var arr = [{ un: 'test', uid: 1 }];
var obj = { un: 'test', uid: 1};
console.log("obj",obj);

will never return anything else than -1 because obj has a different address than the object in arr. That's why, if you push objin arr it does work.

So Array.prototype.indexOf with an object as an argument will look for the same reference, whereas Array.prototype.indexOf with number, string, null will look for the value.

If you want to find the index of an object with specific properties use :

var arr = [{ un: 'test', uid: 1 }];
var index = arr.findIndex(i => i.un === 'test' && i.uid === 1);

Here is the doc for findIndex : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer. I understand. Is there a different method for obtaining the index of the object inside the array without a 'for' loop or some syntax of lodash?
I added the example with .findIndexmethod. You don't need lodash :)
Wonderful. Thank you for your assistance here. Been very helpful. I tried findIndex before but I must have typo'd because it was reporting an invalid function. Sometimes I've noticed that node.js doesn't have all the prototypes that javascript does so I apparently didn't look close enough.
It is true, ECMA standards are ahead of most Javascript implementations. Javascript itself is a specification, like a protocol or a norm, then multiple engines implement it, but obviously like any norm it is always ahead of its implementations. In rare cases, implementations will define the norm, it happended I believe with the Promise class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.