Javascript 在对象数组中查找匹配的对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6237537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:53:25  来源:igfitidea点击:

Finding matching objects in an array of objects?

javascript

提问by fancy

var set = [{"color":"blue"},{"color":"green"},{"color":"red"},{"color":"green"}];

I'd like to be able to do something like a db call, set.find({"color":"green"})and have it return an array full of objects that contain that property.

我希望能够执行诸如 db 调用之类的操作,set.find({"color":"green"})并让它返回一个充满包含该属性的对象的数组。

回答by Domenic

Using Array#filter, for this particular case the code would look like

使用Array#filter,对于这种特殊情况,代码看起来像

var results = set.filter(function (entry) { return entry.color === "green"; });

Array#filteris not implemented in some older browsers, so see the linked article for a backward compatibility shim, or better yet get a full-fledged ES5 shim.

Array#filter某些较旧的浏览器中未实现,因此请参阅链接文章以获取向后兼容性垫片,或者更好地获得完整的 ES5 垫片

For the more general case, it's just a matter of extending this idea:

对于更一般的情况,这只是扩展这个想法的问题:

function findByMatchingProperties(set, properties) {
    return set.filter(function (entry) {
        return Object.keys(properties).every(function (key) {
            return entry[key] === properties[key];
        });
    });
}

var results = findByMatchingProperties(set, { color: "green" });

Again, I am using ECMAScript 5 methods Object.keysand Array#every, so use an ES5 shim. (The code is doable without an ES5 shim but uses manual loops and is much less fun to write and read.)

同样,我使用的ECMAScript 5方法Object.keysArray#every,所以使用ES5垫片。(代码在没有 ES5 shim 的情况下是可行的,但使用手动循环并且编写和阅读的乐趣要少得多。)

回答by Rambabu Bommisetti

I have used map function from jquery and I am getting selected index by passing searched key value so by using that index we will get required object from array.

我使用了 jquery 的 map 函数,我通过传递搜索的键值来获取选定的索引,因此通过使用该索引,我们将从数组中获取所需的对象。

var mydata = [{ name: "Ram", Id: 1 }, { name: "Shyam", Id: 2 }, { name: "Akhil", Id: 3 }];

searchKey = 2

var mydata = [{ name: "Ram", Id: 1 }, { name: "Shyam", Id: 2 }, { name: "Akhil", Id: 3 }];

searchKey = 2

var selectedData = mydata[mydata.map(function (item) { return item.Id; }).indexOf(searchKey)];

console.log(selectedData)

var selectedData = mydata[mydata.map(function (item) { return item.Id; }).indexOf(searchKey)];

console.log(selectedData)

output
{ name: "Shyam", Id: 2 }

Note: if you want to pass search key as object then
searchKey = { Id: 2 };

mydata[mydata.map(function (item) { return item.Id; }).indexOf(searchKey.Id)];

output
{ name: "Shyam", Id: 2 }

回答by Ken Redler

Since you've included the jQuery tag, here's one way to do it using jQuery's map:

由于您已经包含了 jQuery 标记,这里是使用 jQuery 的一种方法map

var results = $.map( set, function(e,i){
  if( e.color === 'green' ) return e; 
});

The documentation states that you need to return nullto remove the element from the array, but apparently this is false, as shown by the jsFiddle in the comments; returning nothing (i.e. returning undefined) works just as well.

文档指出您需要返回null以从数组中删除元素,但显然这是错误的,如注释中的 jsFiddle 所示;不返回任何内容(即返回undefined)也同样有效。