Title: Why is the object not found in the array with indexOf
Tags: javascript
Question: When I search for the object literal below I get for index -1 but I do not understand why.
Can I not use complex types like object literal to search for this object in the array?
var fruits = [{name: "Banana"}, {name: "Orange"}];
var index = fruits.indexOf({name: "Banana"});
Comment: Short answer: Because `{name: "Banana"} === {name: "Banana"}` is false.

Comment: @RobG: It wouldn't make a difference if loose comparison was used. Two objects are always compared by reference.

Comment: Objects cannot be compared in any simple way, here is a related post: http://stackoverflow.com/questions/1068834/object-comparison-in-javascript

Comment: As hinted at in other comments and answers, [*Array.prototype.indexOf*](http://ecma-international.org/ecma-262/5.1/#sec-151.154.11.224) uses the [*strict equality comparison algorithm*](http://ecma-international.org/ecma-262/5.1/#sec-11.9.6) `===`, so object members will only be equal if they reference the same object.

Comment: Helpful solution to the actual problem at http://stackoverflow.com/questions/237104/array-containsobj-in-javascript

Comment: _.some([{name: "Banana"}, {name: "Orange"}], _.isEqual.bind(this, {name: "Banana"}))

Here is the accepted answer: -1 means the {name: "Banana"} was not found in the array. That's because the first {name: "Banana"} is a different object from the second {name: "Banana"}.
If you were searching for a primitive type, it would work the way you are expecting. However, with an object, it looks to see that the actual objects are the same things (i.e., the same locations in memory) and not just that they have the same values.
[0,1,2,3].indexOf(3);     // 3, no surprise
var foo = {name: "Banana"};
[foo].indexOf(foo);       // 0, no surprise
var bar = {name: "Banana"};
[foo].indexOf({name: "Banana"}); // -1, here's your wtf moment,
foo is a different object than bar. They just happen to have the same properties. But they occupy different locations in memory. Or, put another way, if I modify foo, bar remains unchanged.
Lots of libraries have solved the "deep equals" issue for you, so if you're using jQuery or Underscore or something, check there. Otherwise, look up how to implement because there are some gotchas.
Comment for this answer: @dandavis `isEqual()` http://underscorejs.org/#isEqual
Comment for this answer: which underscore command would do what you describe?

Here is another answer: For multi-dimensional arrays, I don't believe you can use indexOf in such a simple way, however, you can use something like this:
for(var i = 0; i < fruits.length; i++) {
if(fruits[i].name === 'Banana') {
return i;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:10/12/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript>', 'Title': 'Why is the object not found in the array with indexOf', 'ViewCount': 41, 'id': 26330559, 'user_AboutMe': '', 'user_AccountId': 1274789.0, 'user_DisplayName': 'HelloWorld', 'user_Reputation': 4424.0, 'user_Views': 629.0, 'user_id': 1230266.0}}