Last active
October 28, 2017 03:42
-
-
Save stephenhandley/95ba5ac3f58aef503436655dc58abe46 to your computer and use it in GitHub Desktop.
derp derp derp generateCsv.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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