使用papaparse遍历特定数据后如何解析结果?

时间:2019-01-10 18:03:23

标签: javascript json csv papaparse

我正在尝试使用PapaParse解析本地CSV文件并遍历JSON,以便仅从文件中输出特定数据。

示例:我只希望“磨粉描述”列与adidas && Champion匹配,所有其他数据都将被丢弃。

如果您需要更多说明,请随时询问,我会定期在这里检查。

CSV文件:https://ufile.io/ze7xl

papa.parse(file, {
  worker: true,
  header: true,
  transformHeader: true,
  step: function(result) {
    var data = result.data;
    for (var i = 0; i < data.length; i++) {
      var millDescription = JSON.stringify(data[i]["Mill Description"]);
      if (
        millDescription.includes("adidas") ||
        millDescription.includes("Champion")
      ) {
      // This is where I need help
      }
    }
  },
  complete: function(result, csv) {
    console.log("parsing complete read: ", result, csv); // Nothing is passed to here yet.
  }
});

2 个答案:

答案 0 :(得分:0)

尝试

 papa.parse(file, {
  worker: true,
  header: true,
  transformHeader: true,
  step: function(result) {
    let data = result.data;
    data = data.filter(d => d["Mill Description"].includes('adidas') || d["Mill Description"].includes('adidaChampions'));
  },
  complete: function(result, csv) {
    console.log("parsing complete read: ", result, csv); // Nothing is passed to here yet.
  }
});

答案 1 :(得分:0)

输出看起来像这样,有多个JSON数组,然后我的问题是如何解析所有数据以转换回csv文件?

[{
      "Item Number": "B12704533",
      "GTIN Number": "190311332942",
      "Mill Code": "04",
      "Mill Description": "adidas Golf",
      "Style Number": "A262",
      "Mill Style Number": "TWA262S8",
      "Style Name": "AD LADS MICRO STRIPE POLO",
      "Color Code": "53",
      "Color Description": "ROYAL",
      "Size Code": "3",
      "Size Description": "S",
      "Unit Weight": ".7258",
      "Cost": "0",
      "CC": "0",
      "CD": "0",
      "FO": "0",
      "KC": "0",
      "MA": "0",
      "PH": "0",
      "TD": "0",
      "CN": "0",
      "WA": "0",
      "GD": "316",
      "Total Inventory": "316"
    }]
    [{
      "Item Number": "B12704534",
      "GTIN Number": "190311332966",
      "Mill Code": "04",
      "Mill Description": "adidas Golf",
      "Style Number": "A262",
      "Mill Style Number": "TWA262S8",
      "Style Name": "AD LADS MICRO STRIPE POLO",
      "Color Code": "53",
      "Color Description": "ROYAL",
      "Size Code": "4",
      "Size Description": "M",
      "Unit Weight": ".7717",
      "Cost": "0",
      "CC": "0",
      "CD": "0",
      "FO": "0",
      "KC": "0",
      "MA": "0",
      "PH": "0",
      "TD": "1",
      "CN": "0",
      "WA": "0",
      "GD": "540",
      "Total Inventory": "541"
    }]