I was looking at some examples of using findIndex(), but I think they gave static examples by setting the search value as static variable inside the function.
I want to find the index of an object in an array so I can call it later. So far how I did it is as follows:
https://jsfiddle.net/osbb5zgc/5/
var array1 = [{name: "Adam", id: 23},{name: "Badam", id: 55}];
var target = 55;
var res = array1.findIndex(function(element){
return element.id === target;
});
// Returns 1
It correctly returns 1 as is the index of {name: "Badam", id: 55}, BUT is there a better way of passing that target-variable into the function? Or will this work in most cases?
indexOffindIndexwon't be a suitable approach or how it can be improved upon. Can you specify your reservations againstfindIndex?