53

if I have an array like:

var myArray = [
                {
                    'color':'red',
                    'name': 'redName'
                },
                {
                    'color':'blue',
                    'name': 'blueName'
                },
                {
                    'color':'green',
                    'name': 'greenName'
                    },
                {
                    'color':'yellow',
                    'name': 'yellowName'
                },
             ];

How do I get the index of say, "blue"?

9 Answers 9

73

If you're already using ECMAScript 5 in your code you can use that:

myArray
    .map(function (element) {return element.color;})
    .indexOf('blue');

Note that the support to these functions is a quite limited (they don't work on Internet Explorer 8).

Also, if you're in the future, and you're using ES6 you can do that:

myArray.map((el) => el.color).indexOf('blue');

That's the same as above, but smaller.

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

3 Comments

this is an elegant solution
"if you're in the future" kudos!
There is a function which simplifies more the code above: myArray.findIndex(el => el.color === 'blue'); It's supported on many modern browsers.
54
for(var i = 0; i < myArray.length; i++) {
   if(myArray[i].color === 'blue') {
     return i;
   }
}

There's no "clean" way unless you want to involve a third-party library. Underscore is a good one for stuff like this.

3 Comments

The link in this answer is broken now.
@AndersonGreen fixed, thank you! also, this answer is pretty old. I recommend Lodash now. ES5 also provides a native find() implementation to the Array prototype.
Would be great if you could add the Lodash function for it
43

I know this is super old but Javascript es6 has an awesome method Array.prototype.findIndex(), so you could just do:

let index = myArray.findIndex((item) => item.color === 'blue');
// value of index will be "1"

1 Comment

and if there's no 'blue' color, the value will be -1 <- use this if you need to make any checks based on the index itself
11

That's not a multi-dimensional array (or even a jagged array, which is used instead of multi-dimensional arrays in Javascript as they don't exist). That is an array of objects.

You can loop through the items in the array:

var index;
for (var i = 0; i < myArray.length; i++) {
  if (myArray[i].color == 'blue') {
    index = i;
    break;
  }
}

Now the variable index contains the value 1, with your given example.

Comments

6

You can use .findIndex() method

In your case

var findeMe = "blue";
myArray.findIndex(function(row){
return row.color == findMe;
});

I hope it help.

Comments

2

I'm guessing you mean from your example to return "1"? Here is a function for you...

<script type='text/javascript'>
var myArray = [
                {
                    'color':'red',
                    'name': 'redName'
                },
                {
                    'color':'blue',
                    'name': 'blueName'
                },
                {
                    'color':'green',
                    'name': 'greenName'
                    },
                {
                    'color':'yellow',
                    'name': 'yellowName'
                },
             ];
function getIndexOf(a,v) {
  var l = a.length;
  for (var k=0;k<l;k++) {
    if (a[k].color==v) {
        return k;
      }
  }                      
  return false;
}

alert (getIndexOf(myArray,'blue'));
</script>

Comments

2

For example like this:

DEMO

Array.prototype.objIndexOf = function(val) {
    var cnt =-1;
    for (var i=0, n=this.length;i<n;i++) {
      cnt++;
      for(var o in this[i]) {
        if (this[i].hasOwnProperty(o) && this[i][o]==val) return cnt;
      }
    }
    return -1;
}
var myArray = [
                {
                    'color':'red',
                    'name': 'redName'
                },
                {
                    'color':'blue',
                    'name': 'blueName'
                },
                {
                    'color':'green',
                    'name': 'greenName'
                    },
                {
                    'color':'yellow',
                    'name': 'yellowName'
                },
             ];
    alert(myArray.objIndexOf('blue'))

2 Comments

1 year old answer and you bother to downvote. Shees. stackoverflow.com/questions/8859828/… Good enough for MDN, good enough for me: developer.mozilla.org/en-US/docs/JavaScript/Reference/…
haha I didnt actually downvote =) since it was already 0. 1 year later and this question was still relevant to something I was looking for.
0

Iterate over the array, looking for the value inside each element object. Raw JavaScript doesn't give you a whole lot to work with.

3 Comments

...I take it there's no indexOf for a multidimensional array?
@redconservatory Well, that's not a multi-dimensional array. But no, there isn't. There are a ton of implementations you could add to the array prototype, or just do it manually.
there is not. But I just want to make sure you have something clear here...it seems your question is to look up the key of a value, which would be like php's array_search(). But "indexOf" is more like php's strpos() in that it looks to see if a string is within another string (and returns position of occurrence).
0

In my case, I have built my own functions because I wanted the loop to stop at the first element found.

That's why the solutions with map were not working to this case as they are creating a new array.

  var i = 0;
  while (i < myArray.length) {
    if (myArray[i].color == 'blue') {
      return i;
    }
    i++
  }
  return -1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.