2014-09-04 27 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