Home > Enterprise >  Is indexOf() type sensitive in javascript?
Is indexOf() type sensitive in javascript?

Time:12-17

I have this function that iterates each object in an array to get their id and then filtering the result using a given id:

const id = another.id; //variable used to filter through the items
const pos = this.items.map(function(e){return e.id}).indexOf(id); //filter items with another.id
this.items.splice(pos, 1) //remove item with the same id as another.id

The another.id would sometimes be a string and sometimes be a number, it would work when the type is string but pos would become -1 indicating that indexOf() didn't find a match. The e.id's are also a typeof string.

So my question would be as the title says, is indexOf() type sensitive?

CodePudding user response:

Yes, it's type sensitive according to mdn:

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

  • Related