How can I find the index of an object inside a Array using underscore.js?

前端 未结 7 815
滥情空心
滥情空心 2021-02-14 07:45

I want to get the index of the given value inside a Array using underscore.js.

Here is my case

var array = [{\'id\': 1, \'name\': \'xxx\'},
                      


        
7条回答
  •  忘了有多久
    2021-02-14 08:37

    Underscore uses native indexOf method if available,else applies fallback. Thus, for a list of objects you have to implement it in some other way.

    One example could be

    _.chain(array).pluck("key").indexOf("value").value();
    

    or

    _.map(array,function(e) { return e.key; }).indexOf(value);
    

提交回复
热议问题