Plain JS: findIndex
The findIndex() method returns an index in the array, if an element in
the array satisfies the provided testing function. Otherwise -1 is
returned.
[{
"Name": "Mercury",
"Temperature": "427°C",
"Position" : 1
}, {
"Name": "Venus",
"Temperature": "462°C",
"Position" : 2
}, {
"Name": "Earth",
"Temperature": "16°C",
"Position" : 3
}].findIndex(x => x.Name === "Earth")
If you're in IE 9+, you can however use the reduce function
The reduce() method applies a function against an accumulator and each
value of the array (from left-to-right) to reduce it to a single
value.
[{
"Name": "Mercury",
"Temperature": "427°C",
"Position" : 1
}, {
"Name": "Venus",
"Temperature": "462°C",
"Position" : 2
}, {
"Name": "Earth",
"Temperature": "16°C",
"Position" : 3
}].reduce(function (foundSoFar, x, i) { // Note no arrow funcion
if (foundSoFar < 0 && x.Name === "Earth") {
return i;
} else {
return foundSoFar;
}
}, -1);
Alternatively, use an implementation of a library like ramda