如果您有一个数组,例如
var people = [
  { "name": "bob", "dinner": "pizza" },
  { "name": "john", "dinner": "sushi" },
  { "name": "larry", "dinner": "hummus" }
];
您可以使用filterArray 对象的方法:
people.filter(function (person) { return person.dinner == "sushi" });
  // => [{ "name": "john", "dinner": "sushi" }]
在较新的 JavaScript 实现中,您可以使用函数表达式:
people.filter(p => p.dinner == "sushi")
  // => [{ "name": "john", "dinner": "sushi" }]
您可以搜索"dinner": "sushi"使用map
people.map(function (person) {
  if (person.dinner == "sushi") {
    return person
  } else {
    return null
  }
}); // => [null, { "name": "john", "dinner": "sushi" }, null]
或 reduce
people.reduce(function (sushiPeople, person) {
  if (person.dinner == "sushi") {
    return sushiPeople.concat(person);
  } else {
    return sushiPeople
  }
}, []); // => [{ "name": "john", "dinner": "sushi" }]
我相信您可以将其推广到任意键和值!