最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Find index of javascript "array of objects" based on object field value - Stack Overflow

programmeradmin1浏览0评论

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.

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 asked May 9, 2014 at 14:38 onkamionkami 9,41119 gold badges104 silver badges197 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 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();

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

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

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])
  }
}
发布评论

评论列表(0)

  1. 暂无评论