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

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

    The simplest solution is to use lodash:

    1. Install lodash:

    npm install --save lodash

    1. Use method findIndex:

    const _ = require('lodash');

    findIndexByElementKeyValue = (elementKeyValue) => {
      return _.findIndex(array, arrayItem => arrayItem.keyelementKeyValue);
    }
    

提交回复
热议问题