2014-09-04 100 views
0
fnd = _.indexOf([{id:1},{id:2},{id:3}], {id:3}); 
console.log(fnd); //2 

如何找到使用underscore.js的键值的索引?如何找到键值为下划线的数组索引?

谢谢

+1

可能重复[找到具有下划线中特定键值的对象的数组索引](http://stackoverflow.com/questions/21631127/find-the-array-index-对象与特定键值在下划线) – thefourtheye 2014-09-04 09:48:59

+0

可能的重复 - http://stackoverflow.com/questions/12356642/is-there-an-indexof-in-javascript-to- search-an-array-with-custom-compare-functio – Swetha 2014-09-04 09:53:09

+0

[有没有在javascript中使用自定义比较函数来搜索数组的indexOf(http://stackoverflow.com/questions/12356642/is-there-一个-的indexOf-中的JavaScript到搜索的阵列与 - 定制 - 比较 - 函数的) – jjj 2017-02-09 09:35:27

回答

0

可以使用lodash的indexOf和采摘此场景等 .indexOf( .pluck([阵列], 'KEY'), 'VALUE') 实施例:

var arr = [{id:1},{id:2},{id:3}]; 
_.indexOf(_.pluck(arr,'id'),3); 

将返回2