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

前端 未结 7 796
滥情空心
滥情空心 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:35

    In case you have complicated objects, and want to search one object in the collection looking for a certain property, just go with:

     _.indexOf(arrayObj, _.findWhere(arrayObj, {id: 1})  );
    

    Where "arrayObj" is the collection with objects, "id" is the prop, and "1" is the value being in search.

提交回复
热议问题