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

In underlining, I can successfully find an element with a specific key value

var tv = [{id:1},{id:2}] var voteID = 2; var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; }); //data = { id: 2 } 

but how do I find which array index occurred in?

+84
javascript
Feb 07 '14 at 15:07
source share
9 answers

I don't know if there is an existing underscore method that does this, but you can achieve the same result with simple javascript.

 Array.prototype.getIndexBy = function (name, value) { for (var i = 0; i < this.length; i++) { if (this[i][name] == value) { return i; } } return -1; } 

Then you can simply do:

var data = tv[tv.getIndexBy("id", 2)]

+28
Feb 07 '14 at 15:32
source share

findIndex was added in 1.8:

 index = _.findIndex(tv, function(voteItem) { return voteItem.id == voteID }) 

See: http://underscorejs.org/#findIndex

Alternatively, this also works if you don't mind making another temporary list:

 index = _.indexOf(_.pluck(tv, 'id'), voteId); 

See: http://underscorejs.org/#pluck

+166
Nov 10 '14 at 15:43
source share

If you want to stay underlined to make your predicate function more flexible, here are 2 ideas.

Method 1

Since the predicate for _.find gets both the value and the index of the element, you can use the side effect to extract the index, for example:

 var idx; _.find(tv, function(voteItem, voteIdx){ if(voteItem.id == voteID){ idx = voteIdx; return true;}; }); 

Method 2

Considering the source of the underscore, this is the _.find way:

 _.find = _.detect = function(obj, predicate, context) { var result; any(obj, function(value, index, list) { if (predicate.call(context, value, index, list)) { result = value; return true; } }); return result; }; 

To make this findIndex function, simply replace the line result = value; on result = index; . This is the same idea as the first method. I included it to emphasize that underlining uses a side effect to implement _.find .

+38
Aug 26 '14 at 10:51
source share

Lo-Dash , which extends Underscore, has a findIndex method that can find the index of a given instance either by a given predicate or according to the properties of this object.

In your case, I would do:

 var index = _.findIndex(tv, { id: voteID }); 

Give it a try.

+34
Dec 30 '15 at 7:46
source share

If your target environment supports ES2015 (or you have a forwarding step, for example with Babel), you can use your own Array.prototype.findIndex ().

Given your example

 const array = [ {id:1}, {id:2} ] const desiredId = 2; const index = array.findIndex(obj => obj.id === desiredId); 
+10
May 19, '17 at 17:59 on
source share

you can use indexOf method from lodash

 var tv = [{id:1},{id:2}] var voteID = 2; var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; }); var index=_.indexOf(tv,data); 
+4
Jun 01. '17 at 11:43 on
source share

Keepin 'it simple:

 // Find the index of the first element in array // meeting specified condition. // var findIndex = function(arr, cond) { var i, x; for (i in arr) { x = arr[i]; if (cond(x)) return parseInt(i); } }; var idIsTwo = function(x) { return x.id == 2 } var tv = [ {id: 1}, {id: 2} ] var i = findIndex(tv, idIsTwo) // 1 

Or, for non-haters, a CoffeeScript variant:

 findIndex = (arr, cond) -> for i, x of arr return parseInt(i) if cond(x) 
+3
Jul 05 '14 at 16:17
source share

If you expect multiple matches and therefore you need an array to return, try:

 _.where(Users, {age: 24}) 

If the property value is unique and you need a compliance index, try:

 _.findWhere(Users, {_id: 10}) 
0
May 05 '15 at 12:51
source share
 Array.prototype.getIndex = function (obj) { for (var i = 0; i < this.length; i++) { if (this[i][Id] == obj.Id) { return i; } } return -1; } List.getIndex(obj); 
0
Feb 14 '17 at 14:48
source share



All Articles