Skip to content

Instantly share code, notes, and snippets.

@vbarbarosh
Last active June 27, 2019 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbarbarosh/749541dabb1a0ab64802639b3e157a41 to your computer and use it in GitHub Desktop.
Save vbarbarosh/749541dabb1a0ab64802639b3e157a41 to your computer and use it in GitHub Desktop.
node_csv_write – Create csv files from arrays https://codescreens.com
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: 'rwillicott0@imageshack.us', name: 'Raffarty Willicott'},
{email: 'aolooney1@senate.gov', name: 'Ange O\'Looney'},
{email: 'bsorey2@bing.com', 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