https://github.com/aichbauer/node-convert-array-to-csv
Convert an array to a csv formatted string
https://github.com/aichbauer/node-convert-array-to-csv
array converter csv list nodejs values
Last synced: 10 months ago
JSON representation
Convert an array to a csv formatted string
- Host: GitHub
- URL: https://github.com/aichbauer/node-convert-array-to-csv
- Owner: aichbauer
- License: mit
- Created: 2018-04-05T10:48:10.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:04:02.000Z (about 3 years ago)
- Last Synced: 2025-03-26T06:43:15.091Z (11 months ago)
- Topics: array, converter, csv, list, nodejs, values
- Language: JavaScript
- Size: 237 KB
- Stars: 31
- Watchers: 2
- Forks: 11
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# convert-array-to-csv
[](https://www.npmjs.com/package/convert-array-to-csv)
[](https://travis-ci.org/aichbauer/node-convert-array-to-csv)
[](https://codecov.io/gh/aichbauer/node-convert-array-to-csv)
> Convert an array to a csv formatted string
## Table of Contents
* [Why?](#why)
* [Installation](#installation)
* [Functions](#functions)
* [Usage](#usage)
* [License](#license)
## Why?
I needed a simple way to download the data from a table component in a csv format.
## Installation
```sh
$ npm i convert-array-to-csv -S
```
or
```sh
$ yarn add convert-array-to-csv
```
## Functions
Take a look into the [usage section](#usage) for a detailed example.
### convertArrayToCSV
> Note: you can also use the default export.
This function converts an array of objects, or an array of arrays into an csv formatted string.
#### Syntax
Returns a new string.
```js
const csv = convertArrayToCSV(data, options);
```
##### Parameters
* data: an array of arrays or an array of objects
* options: a object
* holds two keys: header and separator
* **header**: and array with the name of the columns, default: `undefined`
* **separator**: the character which is the separator in your csv formatted string, default: `','`
## Usage
An example how to use it.
```js
const { convertArrayToCSV } = require('convert-array-to-csv');
const converter = require('convert-array-to-csv');
const header = ['number', 'first', 'last', 'handle'];
const dataArrays = [
[1, 'Mark', 'Otto', '@mdo'],
[2, 'Jacob', 'Thornton', '@fat'],
[3, 'Larry', 'the Bird', '@twitter'],
];
const dataObjects = [
{
number: 1,
first: 'Mark',
last: 'Otto',
handle: '@mdo',
},
{
number: 2,
first: 'Jacob',
last: 'Thornton',
handle: '@fat',
},
{
number: 3,
first: 'Larry',
last: 'the Bird',
handle: '@twitter',
},
];
/*
const csvFromArrayOfObjects = 'number,first,last,handle\n1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,the Bird,@twitter\n';
*/
const csvFromArrayOfObjects = convertArrayToCSV(dataObjects);
/*
const csvFromArrayOfArrays = 'number;first;last;handle\n1;Mark;Otto;@mdo\n2;Jacob;Thornton;@fat\n3;Larry;the Bird;@twitter\n';
*/
const csvFromArrayOfArrays = convertArrayToCSV(dataArrays, {
header,
separator: ';'
});
```
## License
MIT © Lukas Aichbauer