I think you can solve it in one line using the map function:
const pos = myArray.map(e => e.hello).indexOf('stevie');
Answer from Pablo Francisco Pérez Hidalgo on Stack OverflowI think you can solve it in one line using the map function:
const pos = myArray.map(e => e.hello).indexOf('stevie');
Array.prototype.findIndex is supported in all browsers other than IE (non-edge). But the polyfill provided is nice.
var indexOfStevie = myArray.findIndex(i => i.hello === "stevie");
The solution with map is okay. But you are iterating over the entire array every search. That is only the worst case for findIndex which stops iterating once a match is found.
There's not really a concise way (when devs had to worry about IE8), but here's a common solution:
var searchTerm = "stevie",
index = -1;
for(var i = 0, len = myArray.length; i < len; i++) {
if (myArray[i].hello === searchTerm) {
index = i;
break;
}
}
or as a function:
function arrayObjectIndexOf(myArray, searchTerm, property) {
for(var i = 0, len = myArray.length; i < len; i++) {
if (myArray[i][property] === searchTerm) return i;
}
return -1;
}
arrayObjectIndexOf(arr, "stevie", "hello"); // 1
Just some notes:
- Don't use for...in loops on arrays
- Be sure to break out of the loop or return out of the function once you've found your "needle"
- Be careful with object equality
For example,
var a = {obj: 0};
var b = [a];
b.indexOf({obj: 0}); // -1 not found
Videos
Hi everyone, I have been stuck on this for about 10 hours now....
Let me post the code, then explain exactly what I need to do.
Here's my array:myArray[obj, obj, obj];
Here's my objects that are inside the array:obj {name : "keith", uniqueId : 0}obj {name : "keith", uniqueId : 1}obj {name : "keith", uniqueId : 2}
Here's what I need to do:
I need to get the indexOf() these objects based on that uniqueId. The objects will be moved around the array and could be in any position.
myArray[obj, obj, obj];obj {name : "keith", uniqueId : 0} //myArray index 2obj {name : "keith", uniqueId : 1} //myArray index 0obj {name : "keith", uniqueId : 2} //myArray index 1
So how do I do this? I'd like to do something like:indexOf(object with the unique id of 2) // 1
If you're already using ECMAScript 5 in your code you can use that:
myArray
.map(function (element) {return element.color;})
.indexOf('blue');
Note that the support to these functions is a quite limited (they don't work on Internet Explorer 8).
Also, if you're in the future, and you're using ES6 you can do that:
myArray.map((el) => el.color).indexOf('blue');
That's the same as above, but smaller.
for(var i = 0; i < myArray.length; i++) {
if(myArray[i].color === 'blue') {
return i;
}
}
There's no "clean" way unless you want to involve a third-party library. Underscore is a good one for stuff like this.
Maybe you would like to use higher-order functions such as "map". Assuming you want search by 'field' attribute:
var elementPos = array.map(function(x) {return x.id; }).indexOf(idYourAreLookingFor);
var objectFound = array[elementPos];
The simplest and easiest way to find element index in array.
ES5 syntax: [{id:1},{id:2},{id:3},{id:4}].findIndex(function(obj){return obj.id == 3})
ES6 syntax: [{id:1},{id:2},{id:3},{id:4}].findIndex(obj => obj.id == 3)