PapaParse
PapaParse copied to clipboard
transformHeader for unparse?
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);
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);