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

Support object sub-field queries in "id" #74

Closed samdidos closed 3 years ago

samdidos commented 3 years ago

Hi,

I want to be able to query sub-fields in object in order to avoid field collision and have more flexibility on my query.

I could do something like :

const objects = [
        {id: 1, person: {name: "BOB", gender: "Male"}}, 
        {id: 2, person: {name: "ALICE", gender: "Female"}}
    ]

const csv = csvWriter.createObjectCsvWriter({
      path: `path/to/file.csv`,
      header: [
        {
          id: "id", title: "Object ID",
          id: "person.name", title: "Object person name",
          id: "person.gender", title: "Object person gender",
        }
      ],
    });

    await csvWriter.writeRecords(objects);

It could be interesting to support the dot notation like in Lodash get() function.

ryu1kn commented 3 years ago

Hi @samdidos , you can specify headerIdDelimiter for this. Let me know if it doesn't solve your problem 😉

samdidos commented 3 years ago

Oh right cool! Would be nice to have '.' has default value, it's pretty standard. Thank you, you saved me a lot if headache!

ryu1kn commented 3 years ago

This feature was introduced later and I didn't want to break apps for people who have . in their keys :)