Skip to content

Instantly share code, notes, and snippets.

@stephenhandley
Last active October 28, 2017 03:42
Show Gist options
  • Save stephenhandley/95ba5ac3f58aef503436655dc58abe46 to your computer and use it in GitHub Desktop.
Save stephenhandley/95ba5ac3f58aef503436655dc58abe46 to your computer and use it in GitHub Desktop.
derp derp derp generateCsv.js
let data = [
{
a: 1,
b: 2,
c: 3,
d: 10
},
{
a: 2,
b: 3,
c: 10,
e: 11
},
{
a: 1,
c: 10,
f: 11
}
];
function generateCsv ({fields, data}) {
let rows = data.map((row)=> {
return fields.map((field)=> {
return row[field];
}).join(',');
});
let header = fields.join(',');
rows.unshift(header);
return rows.join('\n');
}
let csv = generateCsv({
data,
fields: ['a', 'b', 'c']
});
console.log(csv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment