36

I have a JavaScript array of objects like this:

var myArray = [{...}, {...}, {...}];

Each object has unique id among other properties:

{ 
  id: 4,
  property1: 'something',
  property2: 'something'
}

How can I get an index of a particular object in that array, if I only know its id property? So if I know that myArray[x].id == 4, how can I find x?

1
  • ES6 Array.findIndex - see full example below Commented Jan 31, 2017 at 6:42

7 Answers 7

66
var index = myArray.map(function(el) {
  return el.id;
}).indexOf(4);

For IE below version 9, map need a patch, or just use a loop.

Sign up to request clarification or add additional context in comments.

2 Comments

this looks the best, nice. didn't even think of it
Map should return an array though so this although working, confuses me.For each non matched index of shouldn't there be an element of -1 in the new returned array from map?
21

Or with ES6 syntax:

let index = myArray.map( el => el.id ).indexOf(4)

or

let index = myArray.findIndex( el => el.id === 4 )

Comments

7

Why not simply make a loop ?

function indexOfId(array, id) {
    for (var i=0; i<array.length; i++) {
       if (array[i].id==id) return i;
    }
    return -1;
}

The fact that there are many facilities in js (or js libraries) doesn't mean you must not, sometimes, write a loop. That's fast and simple.

4 Comments

Maybe you should use something other than indexOf
This is likely to cause problems elsewhere in his code; perhaps indexOfId() or something instead?
@Nile: No, indexOf does not work in here - you'd need filter
@Bergi his original answer consisted of the function name indexOf instead of indexOfId
3

ES6 Array.findIndex

const myArray = [{id:1}, {id:2}, {id3}];
const foundIndex = myArray.findIndex((el) => (el.id === 3));

Comments

0

If each id is unique, you can do it like this:

o1 = {id:1}
o2 = {id:2}
o3 = {id:3}
o4 = {id:4}
a = [o1,o2,o3,o4]
a.indexOf( a.filter( function(i){return i.id==4} )[0] );

Comments

0

You could also try a recursive function, though @xdazz's looks rather attractive.

var indexOfId = function(arr, id, index) {
    if (!index) { index = 0; }
    if (arr[index].id == id) {
      return index;
    }
    return ((index += 1) >= arr.length) ? -1 : indexOfId(arr, id, index);
};

Comments

0

You can use .reduce(), which lets you reduce an Array down to a single value.

var obj_idx = myArray.reduce(function(idx, item, i) {
  return item.id === 4 ? i : idx;
}, -1);

The -1 is a default value if no match is found.


If you have multiple uses for this, you may want to make a function factory.

function idxForID(target) {
    return function(idx, item, i) {
      return item.id === target ? i : idx;
    };
}

And then use it like this.

var obj_idx = myArray.reduce(idxForID(4), -1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.