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

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

    May be my suggestion will give you advice.

    Why do you use callback for indexof method? The signature of indexof in underscore.js is the following:

    _.indexOf(array, value, [isSorted]) 
    

    find could be better for this task:

    _.find(array, function(item, index) {
      if (item.id == searchValue.id) {
        alert(index);
      }
    });
    
    0 讨论(0)
提交回复
热议问题