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

时间:2016-05-07 00:25:51

标签: 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",
      "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中实现这一点? 任何帮助将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:0)

那不是JSON,但我认为它是一个部分阵列。

select t.*
from t join
     (select category, min(t2.date) as mindate
      from t2
      where t2.status = 1
     ) t2
     on t2.category = t.category and t.date > t2.mindate;

它是如何运作的?

过滤数组中每个项目的循环(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,则将其添加到新数组中。 value忽略它(过滤掉)