简体   繁体   English

根据对象字段值查找javascript“对象数组”的索引

[英]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()是最干净的方法,但实际上却相当慢。

Benchmarks 基准测试

Article on loops 循环文章

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.

相关问题 Javascript:根据对象上嵌套数组中的值查找数组中对象的索引 - Javascript: Finding index of an object in an array, based on a value within a nested array on the objects 如何通过根据特定字段值对对象进行分组来过滤 javascript object 数组? - How to filter a javascript object array by grouping the objects based on a specific field value? 从对象数组中查找对象索引的最佳方法是什么 - javascript - What is the best way to find index of object from array of objects - javascript javascript:找到正确的索引以将对象放置在对象的排序数组中 - javascript: find correct index to place object in sorted array of objects javascript在对象数组中查找匹配值的索引 - javascript find index of matched value in ay array of objects 对象数组中javascript对象的索引 - Index of javascript object in array of objects 基于键值数组将 javascript object 转换为对象数组 - convert javascript object into array of objects based on key value array javascript在2数组中查找对象的索引 - javascript find index of object in 2 array 根据 88222995402888 属性值从 javascript 对象数组中删除重复项 - Remove duplicates from javascript objects array based on object property value 根据 Javascript 中的属性值从数组的 object 中获取唯一对象 - Getting unique objects from object of array based on property value in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM