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

Accepting nested IDs in header object #34

Closed zoranlorkovic closed 4 years ago

zoranlorkovic commented 4 years ago

As often you need to get data from database and then export it in CSV, it would be very handy if we could put nested IDs in header object.

Example: object = {name: Test, surname: Test, nested: {time: test}}

And our header object would look like: header: [{id: 'name', title: 'First name' }, {id: 'surname', title: 'Last name'}, {id: 'nested.time', title:'My time'}]

Does this make sense?

ryu1kn commented 4 years ago

Thanks @zoranlorkovic for the suggestion. Perhaps we also need to provide an option to overwrite the character used to separate key names as nested.time can actually be a key for someone.

ryu1kn commented 4 years ago

Released as v1.6.0. Let me know if there's any issue 👍

kazeemOnis commented 4 years ago

Hi good day, I just tried

example object:  {
  name: 'SSL Certificate',
  stock: null,
  price: 3650,
  ProductCategory: { 
     name: 'All',
  }
 },
header = [
  { id: 'name', title: 'name' },
  { id: 'ProductCategory.name', title: 'category' },
  { id: 'price', title: 'price' },
  { id: 'stock', title: 'stock' },
 ];

but the category column always comes out empty.

ryu1kn commented 4 years ago

Hi @kazeemOnis , you need to specify headerIdDelimiter: '.'. I didn't want to make . as a default value, as it had introduced a breaking change otherwise.

  • headerIdDelimiter <string> (optional)

    Default: undefined. Give this value to specify a path to a value in a nested object.

https://github.com/ryu1kn/csv-writer/blob/ffd2a2d77426a38d971ed3fa3e58357de7c19f45/README.md#createobjectcsvwriterparams