Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transformHeader for unparse? #1036

Open
MihailsKuzmins opened this issue Jan 24, 2024 · 1 comment
Open

transformHeader for unparse? #1036

MihailsKuzmins opened this issue Jan 24, 2024 · 1 comment

Comments

@MihailsKuzmins
Copy link

MihailsKuzmins commented Jan 24, 2024

According to the documentation (https://www.papaparse.com/docs#unparse-config-default) unparse does not provide "transformHeader" functionality, so it is not possible to customize the headers while preserving the original object. Are there any plans to add this functionality in the future?

For now the only solution is to add these original properties to the original object and specify them as "columns" of UnparseConfig.

const csvOptions: UnparseConfig = {
  columns: ['customHeader'],
  // ... more properties
};

for (const item of items) {
  item['customHeader'] = item['realProperty'];
}

const csvData = Papa.unparse(items, csvOptions);
@vadimcoder
Copy link

You can achieve this with https://github.com/juanjoDiaz/json2csv

    const data = [
      { "carModel": "Audi", "price": 0, "color": "blue" },
      { "carModel": "BMW", "price": 15000, "color": "red", "manual": true },
      { "carModel": "Mercedes", "price": 20000, "color": "yellow" },
      { "carModel": "Porsche", "price": 30000, "color": "green" }
    ];

    const opts = {
      fields: [
        {
          label: 'Car Model',
          value: 'carModel'
        },
        {
          label: 'Price',
          value: "price",
        },
        {
          label: 'Color',
          value: 'color'
        },
        {
          label: 'Manual',
          value: 'manual',
          default: false
        }
      ]
    };


    const json2csvParser = new Parser();
    const csv = json2csvParser.parse(data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants