如何用javascript过滤JSON数据

How can I filter a JSON data in javascript?

本文关键字:JSON 数据 过滤 javascript 何用      更新时间:2023-09-26

我有一个巨大的JSON数据,如下所示。现在,我需要过滤并获取与输入月份相关的json数据的所有属性。

我的JSON数据是:

"maindata" :[
   {
      "month":"multi",
      "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":"multi",
      "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":"multi",
      "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"
   }
]

有人能帮我吗?我该怎么做
我曾尝试使用JSONParse方法,但仅凭这一点还不够。

您可以尝试过滤数据集。

这里,过滤器匹配"multi"的输入值;

您可以将其更改为其他值(甚至根据用户输入使其动态)

// sample data
var data = {
  "maindata": [{
    "month": "multi",
    "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": "multi",
    "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": "multi",
    "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"
  }]
};
// hard-coded - can be set to a dynamic value if need be
var inputMonth = 'multi';
var months = data.maindata.filter(function(elem) {
  if (elem.month === inputMonth) {
    return elem;
  }
});
var results = {};
results['data'] = months;
document.getElementById('myresults').innerHTML = JSON.stringify(results);
//console.log(JSON.stringify(results));
<pre id='myresults'>
</pre>