简体   繁体   English

给定一个对象数组,找到具有特定键的对象

[英]Given an array of objects, find the object with a particular key

Given an array of objects, what is the best way to find the object with a particular key in JS?给定一组对象,在 JS 中找到具有特定键的对象的最佳方法是什么?

Using jQuery and underscoreJS is fine.使用 jQuery 和 underscoreJS 很好。 I'm just looking for the easiest / least code answer.我只是在寻找最简单/最少代码的答案。

Example: An array of objects, where each object has a "name".示例:对象数组,其中每个对象都有一个“名称”。 Find the object with a particular "name".查找具有特定“名称”的对象。

var people = [{name: "A"}, {name: "B"}, {name: "C"}];

My current solution: Pass in the array, the key (eg "name"), and the value (eg "C").我当前的解决方案:传入数组、键(例如“名称”)和值(例如“C”)。

function getObject(myArray, searchKey, searchValue) {
  myArray.forEach(function(element){
    if (element[searchKey] == searchValue) {
      return element;
    }
  });
}

You can use Underscore.js's _.where function, like this你可以使用_.where_.where函数,像这样

console.log(_.where(people, {
    "name": "C"
}));
# [ { name: 'C' } ]

This returns all the objects in the array, which exactly matches the object we pass as the second argument.这将返回数组中的所有对象,它们与我们作为第二个参数传递的对象完全匹配。

Using _.filter :使用_.filter

var people = [{name: "A"}, {name: "B"}, {name: "C"}];

var filteredPeople = _.filter(people, function(obj){
    return obj['name'] == 'C';
});

console.log(JSON.stringify(filteredPeople)) // [{"name":"C"}] 

If you want an array without the matched object(s), use _.reject :如果您想要一个没有匹配对象的数组,请使用_.reject

var filteredPeople = _.reject(people, function(obj){
    return obj['name'] == 'C';
});

console.log(JSON.stringify(filteredPeople)) // [{name: "A"}, {name: "B"}]

You should go with grep of jQuery你应该使用 jQuery 的grep

var people = [{name: "A"}, {name: "B"}, {name: "C"}];

var obj1= jQuery.grep(people,function(n,i){return (n.name=='A')})

Without any custom library you can also do this.没有任何自定义库,您也可以执行此操作。 Please take a look at the following code请看下面的代码

 var people = [{name: "A"}, {name: "B"}, {name: "C"}], match=function(element){ return element.name==="A"; }; console.log(people.filter(match));

But it is kind of static code .但它是一种静态代码。 I don't know how to pass the key and value to the match() function.我不知道如何将键和值传递给 match() 函数。

I'm surprised not to find the obvious answer here, so: To do that with Underscore, you'd use _.find :我很惊讶没有在这里找到明显的答案,所以:要使用_.find做到这一点,您可以使用_.find

function getObject(myArray, searchKey, searchValue) {
    return _.find(myArray, function(entry) { return entry[seachKey] === searchValue; });
}

You don't need Underscore for this, though;不过,您不需要为此使用下划线; JavaScript's array type has find (as of ES5): JavaScript 的数组类型有find (从 ES5 开始):

function getObject(myArray, searchKey, searchValue) {
    return myArray.find(function(entry) { return entry[searchKey] === searchValue; });
}

As of ES2015+, you can make it more concise with an arrow function and destructuring:从 ES2015+ 开始,您可以使用箭头函数和解构使其更加简洁:

function getObject(myArray, searchKey, searchValue) {
    return myArray.find(({[searchKey]: value}) => value === searchValue);
}

Live example of that last one:最后一个的活生生的例子:

 function getObject(myArray, searchKey, searchValue) { return myArray.find(({[searchKey]: value}) => value === searchValue); } const people = [{name: "A"}, {name: "B"}, {name: "C"}]; console.log(getObject(people, "name", "C"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM