Search code examples
javascriptcsvpapaparse

Remove unwanted columns from CSV file using Papaparse


I have a situation where a user can upload a csv file. This CSV file contains a lot of data, but I am only interested in 2 columns (ID and Date). At the moment, I am parsing the CSV using Papaparse

Papa.parse(ev.data, {
    delimiter: "",
    newline: "",
    quoteChar: '"',
    header: true,
    error: function(err, file, inputElem, reason) { },
    complete: function (results) {
        this.parsed_csv = results.data;

    }
});

When this is run this.parsed_csv represents objects of data keyed by the field name. So if I JSON.stringify the output is something like this

[
  {
    "ID": 123456,
    "Date": "2012-01-01",
    "Irrelevant_Column_1": 123,
    "Irrelevant_Column_2": 234,
    "Irrelevant_Column_3": 345,
    "Irrelevant_Column_4": 456
  },
  ...
]

So my main question is how can I get rid of the columns I dont need, and just produce a new csv containing the columns ID and Date?

Thanks

One thing I realised, is there a way to add dynamic variables. For instance I am letting users select the columns I want to map. Now I need to do something like this

let ID = this.selectedIdCol;
this.parsed_csv = results.data.map(element => ({ID: element.ID, Date: element.Date}));

It is saying that ID is unused however. Thanks


Solution

  • let data = [
      {
        "ID": 123456,
        "Date": "2012-01-01",
        "Irrelevant_Column_1": 123,
        "Irrelevant_Column_2": 234,
        "Irrelevant_Column_3": 345,
        "Irrelevant_Column_4": 456
      },
      ...
    ]
    

    just produce results by using the following code:

    data = data.map(element => ({ID: element.ID, Date: element.Date}))
    

    Now you have desired column, please generate a new CSV on these columns