I am using the following code in order to retrieve the index for an object stored in a JavaScript array.
Scripts works fine, but I would like your opinion regarding performance.
Also would like to know also if using [].map() could be more appropriate of [].some().
http://jsbin.com/nasiyiyavi/1/
var data = [{ id: 3 }, { id: 4 }, { id: 1 }, { id: 0 }, { id: 2 }];
function findIndexInData(optionsArg) {
var options = {
data: optionsArg.data,
where: optionsArg.where,
what: optionsArg.what
},
result = -1;
options.data.some(function (item, i) {
if (item[options.where] === options.what) {
result = i;
return true;
}
});
return result;
}
alert(findIndexInData({ data: data, where: 'id', what: 2 }));