arrays-to-csv

2.0.1 • Public • Published

arrays-to-csv

NPM Module to write CSV files o string from Array

You can use this library in your projects to generate csv files from a simple javascript array of objects. This library give us the solution to export csv with diferents delimiters like ',' or ';' without problems.

Install

Using npm in Node.js project

npm install arrays-to-csv

Setup

Once the module is installed in your project, you must import it to be able to use it.

const arrayToCsv = require('arrays-to-csv');

data = [
    {
        field1: 'value1Row1',
        field2: 'value2Row1'
    },
    {
        field1: 'value1Row2',
        field2: 'value2Row2'
    }
]

var csvGenerator = new arrayToCsv(data, { delimiter: ',' });

get the csv String to manage the content in downloads, uploads...

To get the csv string, execute the method.

const arrayToCsv = require('arrays-to-csv');

data = [
    {
        field1: 'value1Row1',
        field2: 'value2Row1'
    },
    {
        field1: 'value1Row2',
        field2: 'value2Row2'
    }
]

//Create the generator
var csvGenerator = new arrayToCsv(data, { delimiter: ',' });

//Execute the method
var csv = csVGenerator.getCsv();

Download csv file to localhost

To download the csv file, indicate the path and execute the saveFile() method.

const arrayToCsv = require('arrays-to-csv');

data = [
    {
        field1: 'value1Row1',
        field2: 'value2Row1'
    },
    {
        field1: 'value1Row2',
        field2: 'value2Row2'
    }
]

//Create the generator
var csvGenerator = new arrayToCsv(data, { delimiter: ',' });

//Execute the method
csVGenerator.saveFile('./data.csv');

Params new arrayToCsv(data, params = {})

Param Default value
delimiter ,
quote "

Package Sidebar

Install

npm i arrays-to-csv

Weekly Downloads

95

Version

2.0.1

License

MIT

Unpacked Size

5.92 kB

Total Files

4

Last publish

Collaborators

  • alvaro.tellez