find the array index of an object with a specific key value in underscore

前端 未结 12 2390
暖寄归人
暖寄归人 2020-12-05 09:12

In underscore, I can successfully find an item with a specific key value

var tv = [{id:1},{id:2}]
var voteID = 2;
var data = _.find(tv, function(voteItem){ r         


        
12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 09:46

    If you're expecting multiple matches and hence need an array to be returned, try:

    _.where(Users, {age: 24})
    

    If the property value is unique and you need the index of the match, try:

    _.findWhere(Users, {_id: 10})
    

提交回复
热议问题