95

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){ return voteItem.id == voteID; });
//data = { id: 2 }

but how do I find what array index that object occurred at?

mheavers
  • 29,530
  • 58
  • 194
  • 315
  • 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) – Bergi Feb 07 '14 at 15:10
  • Thanks - this is useful - but in the example listed you are searching for a single number in an array of numbers - I am looking for a key value in an array of objects. Will this function accommodate that? – mheavers Feb 07 '14 at 15:22
  • 2
    Whether its a numeric test or your property equaility predicate does not matter, yes. – Bergi Feb 07 '14 at 15:24
  • Try `findIndex`: `var dataIndex = _.findIndex(tv, { id: voteID })` – Tom Söderlund Jul 09 '17 at 13:23
  • Please consider accepting [the answer that gives an Underscore solution](https://stackoverflow.com/a/26847450/2493235), as requested in the question. –  Sep 17 '17 at 20:31

13 Answers13

180

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

Ropez
  • 3,485
  • 3
  • 28
  • 30
  • I'm not sure about underscore but in lodash you don't need an anonymous function, simply `index = _.findIndex(tv, {id: voteID})` will work, also works if the `tv` collection has more complicated values (more than just an `id` property) – Scott Jungwirth Oct 09 '15 at 21:25
  • 1
    Just a point using pluck is so much slower. I because of its extra traverse. https://jsperf.com/compare-find-index – Farshid Saberi Nov 08 '16 at 06:26
44

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

In your case, I would do:

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

Give it a try.

splintor
  • 9,924
  • 6
  • 74
  • 89
38

If you want to stay with underscore so your predicate function can be more flexible, here are 2 ideas.

Method 1

Since the predicate for _.find receives both the value and index of an element, you can use side effect to retrieve the index, like this:

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

Method 2

Looking at underscore source, this is how _.find is implemented:

_.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 a findIndex function, simply replace the line result = value; with result = index; This is the same idea as the first method. I included it to point out that underscore uses side effect to implement _.find as well.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
lastoneisbearfood
  • 3,955
  • 5
  • 26
  • 25
28

I don't know if there is an existing underscore method that does this, but you can achieve the same result with plain 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 just do:

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

tewathia
  • 6,890
  • 3
  • 22
  • 27
13

If your target environment supports ES2015 (or you have a transpile step, eg with Babel), you can use the native Array.prototype.findIndex().

Given your example

const array = [ {id:1}, {id:2} ]
const desiredId = 2;
const index = array.findIndex(obj => obj.id === desiredId);
craigmichaelmartin
  • 6,091
  • 1
  • 21
  • 25
4

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);
Amil Sajeev
  • 270
  • 2
  • 9
3

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, the CoffeeScript variant:

findIndex = (arr, cond) ->
  for i, x of arr
    return parseInt(i) if cond(x)
joyrexus
  • 390
  • 1
  • 3
  • 8
2

This is to help lodash users. check if your key is present by doing:

hideSelectedCompany(yourKey) {
 return _.findIndex(yourArray, n => n === yourKey) === -1;
}
Syed
  • 15,657
  • 13
  • 120
  • 154
1

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);
}
Jackkobec
  • 5,889
  • 34
  • 34
0

If you're expecting multiple matches and hence need an array to be returned, try:

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

If the property value is unique and you need the index of the match, try:

_.findWhere(Users, {_id: 10})
Ketan
  • 1,467
  • 13
  • 16
0
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

I got similar case but in contrary is to find the used key based on index of a given object's. I could find solution in underscore using Object.values to returns object in to an array to get the occurred index.

var tv = {id1:1,id2:2};
var voteIndex = 1;
console.log(_.findKey(tv, function(item) {
  return _.indexOf(Object.values(tv), item) == voteIndex;
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
eQ19
  • 9,880
  • 3
  • 65
  • 77
0

And If you want some particular key value from an object by id then use

var tv = [{id:1, name:"ABC"},{id:2, name:xyz}]

_.find(tv , {id:1}).value // retrun "ABC"
Sachin from Pune
  • 656
  • 8
  • 19