Javascript 搜索匹配属性的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2166765/
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-22 23:02:54  来源:igfitidea点击:

Search an array for matching attribute

javascript

提问by Chap

I have an array, I need to return a restaurant's name, but I only know the value of its "food" attribute (not it's index number).

我有一个数组,我需要返回餐厅的名称,但我只知道其“食物”属性的值(而不是索引号)。

For example, how could I return "KFC" if I only knew "chicken"?

例如,如果我只知道“鸡肉”,我怎么能返回“肯德基”?

restaurants = 
  [
    {"restaurant" : { "name" : "McDonald's", "food" : "burger" }},
    {"restaurant" : { "name" : "KFC",        "food" : "chicken" }},
    {"restaurant" : { "name" : "Pizza Hut",  "food" : "pizza" }}
  ];

回答by Matthew Flaschen

for(var i = 0; i < restaurants.length; i++)
{
  if(restaurants[i].restaurant.food == 'chicken')
  {
    return restaurants[i].restaurant.name;
  }
}

回答by He Nrik

In this case i would use the ECMAscript 5 Array.filter. The following solution requires array.filter() that doesn't exist in all versions of IE.

在这种情况下,我将使用 ECMAscript 5 Array.filter。以下解决方案需要在所有版本的 IE 中都不存在的 array.filter()。

Shims can be found here: MDN Array.filteror ES5-shim

垫片可以在这里找到:MDN Array.filterES5-shim

var result = restaurants.filter(function (chain) {
    return chain.restaurant.food === "chicken";
})[0].restaurant.name;

回答by chenkehxx

you can also use the Array.findfeature of es6. the doc is here

你也可以使用Array.find的功能es6。文档在这里

return restaurants.find(item => {
   return item.restaurant.food == 'chicken'
})

回答by Ben

for (x in restaurants) {
    if (restaurants[x].restaurant.food == 'chicken') {
        return restaurants[x].restaurant.name;
    }
}

回答by laVie

Must be too late now, but the right version would be:

现在肯定为时已晚,但正确的版本是:

for(var i = 0; i < restaurants.restaurant.length; i++)
{
  if(restaurants.restaurant[i].food == 'chicken')
  {
    return restaurants.restaurant[i].name;
  }
}

回答by Jhankar Mahbub

you can use ES5 some. Its pretty first by using callback

你可以用 ES5 一些。它首先使用回调

function findRestaurent(foodType) {
    var restaurant;
    restaurants.some(function (r) {
        if (r.food === id) {
            restaurant = r;
            return true;
        }
   });
  return restaurant;
}

回答by Hakan Bilgin

@Chap - you can use this javascript lib, DefiantJS (http://defiantjs.com), with which you can filter matches using XPath on JSON structures. To put it in JS code:

@Chap - 您可以使用这个 javascript 库 DefiantJS ( http://defiantjs.com),您可以使用它在 JSON 结构上使用 XPath 过滤匹配项。把它放在JS代码中:

var data = [
   { "restaurant": { "name": "McDonald's", "food": "burger" } },
   { "restaurant": { "name": "KFC",        "food": "chicken" } },
   { "restaurant": { "name": "Pizza Hut",  "food": "pizza" } }
].
res = JSON.search( data, '//*[food="pizza"]' );

console.log( res[0].name );
// Pizza Hut

DefiantJS extends the global object with the method "search" and returns an array with matches (empty array if no matches were found). You can try out the lib and XPath queries using the XPath Evaluator here:

DefiantJS 使用“search”方法扩展全局对象并返回一个包含匹配项的数组(如果没有找到匹配项,则为空数组)。您可以在此处使用 XPath Evaluator 尝试 lib 和 XPath 查询:

http://www.defiantjs.com/#xpath_evaluator

http://www.defiantjs.com/#xpath_evaluator