Javascript indexOf on an array of objects

If you’re already using ECMAScript 5 in your code you can use that:

myArray
    .map(function (element) {return element.color;})
    .indexOf('blue');

Note that the support to these functions is a quite limited (they don’t work on Internet Explorer 8).

Also, if you’re in the future, and you’re using ES6 you can do that:

myArray.map((el) => el.color).indexOf('blue');

That’s the same as above, but smaller.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)