Why Array.Indexof Doesn't Find Identical Looking Objects

js array.IndexOf not working with objects?

According to MDN:

indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).

That's why that possibleTimes.indexOf always results in -1.

Why doesn't indexOf find an array within the searched array? (Need explanation not solution)

Because

indexOf() compares searchElement to elements of the Array using strict
equality (the same method used by the === or triple-equals operator).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Description

and

Two distinct objects are never equal for either strict or abstract
comparisons.
An expression comparing Objects is only true if the operands reference the same Object.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Using_the_Equality_Operators

Why indexOf can find the index of my object inside array of same class objects?

First thing you gotta know about Objects (in javascript) is that they are assigned by REFERENCE and NOT by value. With that in mind, every time you declare a variable and assign it with a javascript object, you are storing the reference to the object's address in memory.

when you use indexOf with javascript objects, you are comparing memory address of the OBJECTS and not from the CLASS. Everytime you use NEW with a class (that returns a javascript object) you are instantiating an object from a specific class and returning the address of the OBJECT that you can store at a variable (in this case, it is 'n').

When you use indexOf(n), the function will search for the memory address of the object stored in 'n' and return it's index location inside the array. The others objects you created inside the loop (even being from the same class) will have an unique memory address for each one.

The reason why it will work with both == and === is that When you use === you will compare objects address to check if they are equal. When you use ==, it will make a type cast before comparing. Example, if you compare a string with a number using ==, it will convert one type into another and then will compare their values.

IndexOf indicates that an object doesn't exist, even though it should

Alter your method slightly, using the Array.findIndex method to get what you want. The lodash _.findIndex() achieves the same thing, if you use lodash:

vm.submitEmployment = function (role) {

// the idx variable should hold your index number
var idx = $scope.activities.findIndex((item) => $scope.role.Id === item.Id);

}

indexOf method in an object array?

I think you can solve it in one line using the map function:

const pos = myArray.map(e => e.hello).indexOf('stevie');

Javascript indexOf for an array of arrays not finding array

That's because you're searching for a different object. indexOf() uses strict equality comparisons (like the === operator) and [3, 0] === [3, 0] returns false.

You'll need to search manually. Here's an example using a more generic indexOf() function that uses a custom comparer function (with an improvement suggested by @ajax333221 in the comments):

// Shallow array comparer
function arraysIdentical(arr1, arr2) {
var i = arr1.length;
if (i !== arr2.length) {
return false;
}
while (i--) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}

function indexOf(arr, val, comparer) {
for (var i = 0, len = arr.length; i < len; ++i) {
if ( i in arr && comparer(arr[i], val) ) {
return i;
}
}
return -1;
}

var tw = [[3, 0], [11, 0], [3, 14], [11, 14]];
alert( indexOf(tw, [3, 0], arraysIdentical) ); // Alerts 0


Related Topics



Leave a reply



Submit