[英]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. 我有一个对象的Javascript数组,我想在特定对象的字段与我的搜索条件匹配的地方找到该数组元素(对象)的索引。
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. 并且我想定位元素的id字段等于15的元素索引。我正在使用angular和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: array.filter是JavaScript实现这一目标的更优雅的方法:
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
使用ES6,您可以使用
Array#findIndex
The
findIndex()
method returns the index of the first element in the array that satisfies the provided testing function.findIndex()
方法返回满足提供的测试功能的数组中第一个元素的索引 。 Otherwise-1
is returned.否则返回
-1
。
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. 当然,您将不想像我一样使用变量
key
match
静态match
,但是我将不得不更多地了解您的代码来帮助您。
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. 有很多源,但是while循环似乎是迭代数组的最快方法。 Alternatively, Array.map() is in my opinion the cleanest way but actually turns out to be fairly slow.
另外,我认为Array.map()是最干净的方法,但实际上却相当慢。
var array = []; // your array
var len = array.length;
while (len-- ) {
if ( array[i].id == 15 ) {
console.log(array[i])
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.