51

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"?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
redconservatory
  • 21,438
  • 40
  • 120
  • 189

9 Answers9

71

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.

Gustavo Rodrigues
  • 3,517
  • 1
  • 25
  • 21
53
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.

John Strickler
  • 25,151
  • 4
  • 52
  • 68
40

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"
  • 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 – Bullsized Dec 14 '22 at 15:11
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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
6

You can use .findIndex() method

In your case

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

I hope it help.

Webby
  • 333
  • 3
  • 5
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>
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
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'))
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1 year old answer and you bother to downvote. Shees. http://stackoverflow.com/questions/8859828/javascript-what-dangers-are-in-extending-array-prototype Good enough for MDN, good enough for me: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter – mplungjan Oct 18 '12 at 20:12
  • 1
    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. – qodeninja Oct 18 '12 at 20:17
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
AymericFi
  • 140
  • 1
  • 8
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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • ...I take it there's no indexOf for a multidimensional array? – redconservatory Nov 29 '11 at 15:31
  • 2
    @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. – Dave Newton Nov 29 '11 at 15:34
  • 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). – CrayonViolent Nov 29 '11 at 15:36