简体   繁体   中英

Find index of javascript “array of objects” based on object field value

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.

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

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

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

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

Try this:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM