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

前端 未结 12 2395
暖寄归人
暖寄归人 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:44

    I got similar case but in contrary is to find the used key based on index of a given object's. I could find solution in underscore using Object.values to returns object in to an array to get the occurred index.

    var tv = {id1:1,id2:2};
    var voteIndex = 1;
    console.log(_.findKey(tv, function(item) {
      return _.indexOf(Object.values(tv), item) == voteIndex;
    }));

提交回复
热议问题