indexOf method in an object array?

前端 未结 27 2492
别跟我提以往
别跟我提以往 2020-11-22 02:18

What\'s the best method to get the index of an array which contains objects?

Imagine this scenario:

var hello = {
    hello: \'world\',
    foo: \'ba         


        
27条回答
  •  臣服心动
    2020-11-22 02:32

    I like Pablo's answer, but Array#indexOf and Array#map don't work on all browsers. Underscore will use native code if it's available, but has fallbacks as well. Plus it has the pluck method for doing exactly what Pablo's anonymous map method does.

    var idx = _.chain(myArray).pluck("hello").indexOf("Stevie").value();
    

提交回复
热议问题