Search code examples
javascriptjsoncsvpapaparse

How to unparse result after iterating through specific data with papaparse?


I'm trying to use PapaParse to parse a local CSV file and iterate through the JSON in order to only output specific data from the file.

Example: I only want Column "Mill Description" to match adidas && Champion, all other data is discarded.

If you need more clarification don't hesitate to ask, I will be checking here periodically.

CSV File: 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.
  }
});

Solution

  • try this

     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.
      }
    });