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 badges7 Answers
Reset to default 8You'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])
}
}