Last active
June 27, 2019 20:12
-
-
Save vbarbarosh/749541dabb1a0ab64802639b3e157a41 to your computer and use it in GitHub Desktop.
node_csv_write – Create csv files from arrays https://codescreens.com
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
const {createObjectCsvWriter} = require('csv-writer'); | |
// Create csv files from arrays | |
main().catch(panic); | |
// https://stackabuse.com/reading-and-writing-csv-files-with-node-js/ | |
async function main() | |
{ | |
const rows = [ | |
{email: '[email protected]', name: 'Raffarty Willicott'}, | |
{email: '[email protected]', name: 'Ange O\'Looney'}, | |
{email: '[email protected]', name: 'Bryna Sorey'}, | |
]; | |
await csv_write('out.csv', rows); | |
} | |
function csv_write(path, rows) | |
{ | |
const header = Object.keys(rows[0]).map(s => ({id: s, title: s})); | |
return createObjectCsvWriter({path, header}).writeRecords(rows); | |
} | |
function panic(error) | |
{ | |
console.error(error); | |
process.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment