Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a Javascript array of objects and I would like to find index of the array element (object) where particular object's field is matching my search criteria.

That is, array looks like

  [{id:1, saved:0, name: "name1"}, {id:26, saved:0, name: "name2"},
    {id:3, saved:0, name: "name3"}, {id:15, saved:0, name: "name4"}]

and I would like to locate index of the element where element's id field is equal to, say, 15. I am using angular and jquery.

share|improve this question

6 Answers 6

up vote 4 down vote accepted

You'd have to iterate, here's a very simple example

var index = null;

for (var i=0; i<array.length; i++) {
    if ( array[i].id == 15 ) {
        index = i;
        break;
    }
}

That gets you the index, if you just want to return the object you can do

var obj = array.filter(function(obj) {
    return obj.id == 15;
}).shift();
share|improve this answer
1  
I'd also break; after the index = i, just for performance. –  Chris Dixon May 9 at 14:41
1  
@ChrisDixon - Sounds like a good idea. –  adeneo May 9 at 14:42

array.filter is JavaScript's more elegant method for achieving this:

var arrr = [{id:1, saved:0, name: "name1"}, {id:26, saved:0, name: "name2"},
{id:3, saved:0, name: "name3"}, {id:15, saved:0, name: "name4"}];

return arrr.filter( function( value ){ return value.id == 15; })[0];
share|improve this answer

Try this:

$.each(json.yourrootjson, function(i, v) {
    if (v.id == "15") {
        alert(v.id);
        alert(v.name);
        alert(v.saved);
        return;
    }
});
share|improve this answer

There are multiple ways to do this.

You could iterate through the array and do a search.

var search = 15;
for(var index=0; index<input.length; index++) {
   if(input[index].id == search) {
       //Do whatever you want to do with this index.
   }
}

Or you could create a lookup first

var lookup = {};
for(var index=0; index<input.length; index++) {
   var element = input[index];
   lookup[element.id] = element;
   lookup[element.id].index = index;
}

Now, in every subsequent look-ups of a search term, you could do the following

var search = 15;
if(lookup[search]) {
   var index = lookup[search].index;
}
share|improve this answer

Here is a very basic way to do it. Of course, you won't want to use the variables key and match statically like I did, but I will have to know more about your code to help you with that as well.

var array = [{id:1, saved:0, name: "name1"}, {id:26, saved:0, name: "name2"},
             {id:3, saved:0, name: "name3"}, {id:15, saved:0, name: "name4"}]

var key = 'id';
var match = 15;

array.forEach(function(elem, i) {
    if (elem[key] === match) {
        alert(i);
    }
});
share|improve this answer

There are a lot of sources out there but a while loop appears to be the fastest way to iterate an array. Alternatively, Array.map() is in my opinion the cleanest way but actually turns out to be fairly slow.

Benchmarks

Article on loops

var array = []; // your array
var len = array.length;

while (len-- ) {
  if ( array[i].id == 15 ) {
    console.log(array[i])
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.