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
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)
You would have to compare the properties. Something like this would work:
var arr = [];
arr[0] = {a: 1, b: 2};
arr[1] = {a: 1, b: 3};
console.log(arr.findIndex(o => o.a === 1 && o.b === 2));
//0
console.log(arr.findIndex(o => o.a === 1 && o.b === 3));
//1
The array.findIndex() function will bring back the index for the first value that the comparing function results are true. I have a function that I use to compare if two objects are equal that can be used in this example.
var obj = {a: 1, b: 2};
var arr = [];
arr[0] = {a: 1, b: 2};
arr[1] = {a: 1, b: 3};
var idx = arr.findIndex(element => areObjsEqual(element,obj));
console.log(`The index is: ${idx}`);
//Function to check if two js objects are equal
function areObjsEqual(a, b) {
// Create arrays of property names
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
// If number of properties is different,
// objects are not equivalent
if (aProps.length != bProps.length) { return false;}
//loop through the object and compare the property values
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
// If values of same property are not equal,
// objects are not equivalent
if (a[propName] !== b[propName]) {
return false;
}
}
// If we made it this far, objects
// are considered equivalent
return true;
}