Find index of an object with a specific value stored in an array 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 }));
I think the best way of doing that would be using the new ES6 function Array.prototype.findIndex:

data.findIndex(function(obj) { return obj[where] === what; })


If you don't have that function, there are good polyfills at MDN and NPM.

(if you do have full ES6, you apparently could also do

data.findIndex(obj =&gt; obj[where] === what)


but i'm still learning how that syntax works so i may be wrong there)