Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过特定字段过滤JSON数据?_Javascript_Json_Parsing - Fatal编程技术网

Javascript 如何通过特定字段过滤JSON数据?

Javascript 如何通过特定字段过滤JSON数据?,javascript,json,parsing,Javascript,Json,Parsing,我是这里的新手,正在学习JS和JSON。 我有JSON数据,如下所示: { "month":"november", "category":"coffee", "price":50, "name":"Pike Place Roast Brewed Coffee Verismo Pods", "flavor":"flavored", "count":5, "roast":"medium", "typ

我是这里的新手,正在学习JS和JSON。 我有JSON数据,如下所示:

   {
      "month":"november",
      "category":"coffee",
      "price":50,
      "name":"Pike Place Roast Brewed Coffee Verismo Pods",
      "flavor":"flavored",
      "count":5,
      "roast":"medium",
      "type":"regular"
   },
   {
      "month":"august",
      "category":"coffee",
      "price":40,
      "name":"Starbucks VIA Ready Brew French Roast",
      "flavor":"flavored",
      "count":548,
      "roast":"blonde",
      "type":"decaffinated"
   },
   {
      "month":"november",
      "category":"coffee",
      "price":50,
      "name":"Starbucks Caffé Verona Blend, Whole Bean",
      "flavor":"flavored",
      "count":5,
      "roast":"medium",
      "type":"regular"
   },
   {
      "month":"asia-pacific",
      "category":"coffee",
      "price":20,
      "name":"Starbucks Caffè Verona K-Cup Pods",
      "flavor":"flavored",
      "count":3,
      "roast":"dark",
      "type":"regular"
   },
   {
      "month":"august",
      "category":"coffee",
      "price":40,
      "name":"Milk Verismo Pods",
      "flavor":"flavored",
      "count":233,
      "roast":"blonde",
      "type":"decaffinated"
   },
   {
      "month":"november",
      "category":"coffee",
      "price":50,
      "name":"Starbucks VIA Ready Brew Decaf Italian Roast",
      "flavor":"flavored",
      "count":5,
      "roast":"medium",
      "type":"regular"
   },
   {
      "month":"august",
      "category":"coffee",
      "price":40,
      "name":"Guatemala Antigua Espresso Verismo Pods",
      "flavor":"flavored",
      "count":587,
      "roast":"blonde",
      "type":"decaffinated"
   }
现在假设我想获得与月份(如11月)相关的所有数据,我如何用Javascript实现这一点? 任何帮助都将不胜感激。
谢谢。

这不是JSON,但我假设它是部分数组

var result = theArray.filter(function(value){
  return value.month === "November";   // or whatever you filter on
});

// result is now a filter array containing only month === 'November'
它是如何工作的


过滤器在数组值中的每个项上循环。如果返回true,则会将其添加到新数组中。false忽略过滤掉的它,这不是JSON,但我假设它是一个部分数组

var result = theArray.filter(function(value){
  return value.month === "November";   // or whatever you filter on
});

// result is now a filter array containing only month === 'November'
它是如何工作的


过滤器在数组值中的每个项上循环。如果返回true,则会将其添加到新数组中。false忽略了过滤掉的内容

非常感谢!这对我很有帮助:非常感谢!这帮助了我: