1

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.

7 Answers 7

8

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();
Sign up to request clarification or add additional context in comments.

Comments

5

With ES6, you could use Array#findIndex

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

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" }],
    index = array.findIndex(({ id }) => id === 15);

console.log(index);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

2

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];

1 Comment

You can get the item this way, but how can I get the index of that item in the array?
2

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;
}

Comments

0

Try this:

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

Comments

0

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);
    }
});

Comments

0

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])
  }
}

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.