Convert JS Objects Into CSV Strings – objects2csv

Category: Javascript | January 31, 2020
Author:HTLFabian
Views Total:88 views
Official Page:Go to website
Last Update:January 31, 2020
License:MIT

Preview:

Convert JS Objects Into CSV Strings – objects2csv

Description:

objects2csv is a small and modern (ES 7) JavaScript library that converts an array of JS objects into a CSV string for further use.

More Features:

  • Shows header or not
  • Custom string if the value does not exist
  • Custom separator
  • Allows you to exclude keys

How to use it:

1. Install & download the package via NPM.

# NPM
$ npm install objects2csv --save

2. Convert JS objects you provide into a CSV string.

// import
const convert = require('objects2csv');

// data
const cars = [
      { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
      { "name":"BMW", "models":[ "320", "X3", "X5" ] },
      { "name":"Fiat", "models":[ "500", "Panda" ] }
]

// convert
const csv = convert(cars);

// output
console.log(csv);

3. Possible option to config the objects 2 CSV converter.

const csv = convert(cars,{
      header: true, 
      unavailable: " ",
      separator: ";",
      quote: "\"", 
      escape: "\\",
      lineSeparator: "\n",
      excludeKeys: []
});

You Might Be Interested In:


Leave a Reply