0
fnd = _.indexOf([{id:1},{id:2},{id:3}], {id:3});
console.log(fnd); //2

How can i find index of with key-value using underscore.js?

Thank you

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 1
    possible duplicate of [find the array index of an object with a specific key value in underscore](http://stackoverflow.com/questions/21631127/find-the-array-index-of-an-object-with-a-specific-key-value-in-underscore) – thefourtheye Sep 04 '14 at 09:48
  • Possible duplicate - http://stackoverflow.com/questions/12356642/is-there-an-indexof-in-javascript-to-search-an-array-with-custom-compare-functio – Swetha Sep 04 '14 at 09:53
  • Possible duplicate of [Is there an indexOf in javascript to search an array with custom compare function](http://stackoverflow.com/questions/12356642/is-there-an-indexof-in-javascript-to-search-an-array-with-custom-compare-functio) – jjj Feb 09 '17 at 09:35

2 Answers2

0
let index = _.findIndex(fnd, (item) => { 
    return item.id == 2
})

For further reading check out the documentation of the method.

http://underscorejs.org/#findIndex

upq
  • 21
  • 1
  • 5
0

You can use lodash indexOf and pluck for this scenario like .indexOf(.pluck([Array],'KEY'),'VALUE') Example:

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

will return 2