Skip to content

Instantly share code, notes, and snippets.

@rwilcox
Created July 22, 2014 15:54
Show Gist options
  • Select an option

  • Save rwilcox/971bc86d8b9487743e0a to your computer and use it in GitHub Desktop.

Select an option

Save rwilcox/971bc86d8b9487743e0a to your computer and use it in GitHub Desktop.
Using the CSV NPM module, version 0.2.0, turn objects into CSV
# An example project showing how to write objects using the CSV npm package
#
# Uses the CSV 0.2.0 style syntax. For an example that shows CSV 0.4.0
# (major syntax change) see https://gist.github.com/rwilcox/e8411242ffe4f06af35c#file-objects_to_csv-coffee
csv = require 'csv'
util = require 'util'
# Use the callback API to write this
callback_generate_csv_from_list = (records_to_write, cb) ->
map_reduce_it = []
for current_profile in profile_list
map_reduce_it.push( [current_profile.name
, current_profile.category_name
, current_profile.brand?.name] )
csv().from(map_reduce_it).to(cb, {columns: ["Name", "Category", "Brand"], header: true})
profile_list = [ {name: "Macbook Pro, Retina", category_name: "Computer", brand: {name: "Apple"}},
{name: "Macbook Pro", category_name: "Computer", brand: {name: "Apple"}},
{name: "Acer 1", category_name: "Netbook", brand: {name: "Acer"}} ]
callback_generate_csv_from_list profile_list, (data) ->
process.stdout.write data
process.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment