ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

Default values #24

Closed GeoNicol closed 5 years ago

GeoNicol commented 5 years ago

How can I add default values to my csv?

ryu1kn commented 5 years ago

Hi @GeoNicol , default field values? Do you want to explain how you want to write the code and desirable output?

GeoNicol commented 5 years ago

let's say we have these headers header: [ {id: 'name', title: 'NAME'}, {id: 'lang', title: 'LANGUAGE'} ] and my records:

const records = [ {name: 'Bob', lang: 'French, English'}, {name: 'Mary', lang: 'English'}, {name:'Foo'} // This guy doesn't have language ];

I'd love to be able to set a default value for 'lang' so it doesn't show up empty in my csv

ryu1kn commented 5 years ago

Hmm... I think that's something we can do outside of the library. I want to keep the library functionality small and focused.

Can you do something like this?

const defaultValues = {lang: 'Japanese'}
const finalRecords = records.map(record => Object.assign({}, defaultValues, record))

csvWriter.writeRecords(finalRecords)
GeoNicol commented 5 years ago

Thank you for this, I haven't thought about it.

Geo P. Nicolaidis VP Business Development

+357 99231250 | +357 25254113 | xamplifier.com http://www.xamplifier.com/ Eden Beach Hotel #601, 3035 Limassol, Cyprus

On Fri, Apr 26, 2019 at 4:04 AM Ryuichi Inagaki notifications@github.com wrote:

Hmm... I think that's something we can do outside of the library. I want to keep the library functionality small and focused.

Can you do something like this?

const defaultValues = {lang: 'Japanese'}const finalRecords = records.map(record => Object.assign({}, defaultValues, record)) csvWriter.writeRecords(finalRecords)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ryu1kn/csv-writer/issues/24#issuecomment-486889021, or mute the thread https://github.com/notifications/unsubscribe-auth/ADZPI54DJTNER7YK343QTULPSJILVANCNFSM4HIMV5NQ .

ryu1kn commented 5 years ago

No worries! 👍