# convert-array-to-csv

> Convert an array to a csv formatted string

Latest version **2.0.0** (published 2020-05-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install convert-array-to-csv
pnpm add convert-array-to-csv
yarn add convert-array-to-csv
bun add convert-array-to-csv
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2020-05-17 |
| First published | 2018-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/convert-array-to-csv) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 30 |
| Author | Aichbauer Lukas |
| Maintainers | aichbauer |
| Keywords | array, csv, list, convert, comma-separated, values, convert-array-to-csv |

## Links

- npm: https://www.npmjs.com/package/convert-array-to-csv
- Repository: https://github.com/aichbauer/node-convert-array-to-csv
- Homepage: https://github.com/aichbauer/node-convert-array-to-csv#readme
- Issues: https://github.com/aichbauer/node-convert-array-to-csv/issues
- npm.io page: https://npm.io/package/convert-array-to-csv

## Alternatives

- [csv-to-markdown-table](https://npm.io/package/csv-to-markdown-table.md) — 47.0K weekly downloads
- [@sapphire/ratelimits](https://npm.io/package/@sapphire/ratelimits.md) — 4.4K weekly downloads
- [js-csvparser](https://npm.io/package/js-csvparser.md) — 2.0K weekly downloads
- [@adadapted/js-sdk](https://npm.io/package/@adadapted/js-sdk.md) — 251 weekly downloads
- [@grapecity/spread-sheets-sparklines](https://npm.io/package/@grapecity/spread-sheets-sparklines.md) — 103 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2020-05-17
- 1.0.12 — 2020-05-07
- 1.0.11 — 2020-04-09
- 1.0.10 — 2020-02-14
- 1.0.9 — 2019-08-02
- 1.0.8 — 2019-05-25
- 1.0.7 — 2019-04-25
- 1.0.6 — 2019-03-29
- 1.0.5 — 2019-02-26
- 1.0.4 — 2018-09-07
- 1.0.3 — 2018-09-07
- 1.0.2 — 2018-06-22
- 1.0.1 — 2018-05-06
- 1.0.0 — 2018-04-05

## README

# convert-array-to-csv

[![npm](https://img.shields.io/npm/v/convert-array-to-csv.svg?style=flat-square)](https://www.npmjs.com/package/convert-array-to-csv)
[![Travis branch](https://img.shields.io/travis/aichbauer/node-convert-array-to-csv/master.svg?style=flat-square)](https://travis-ci.org/aichbauer/node-convert-array-to-csv)
[![Codecov branch](https://img.shields.io/codecov/c/github/aichbauer/node-convert-array-to-csv/master.svg?style=flat-square)](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

---
_Source: https://npm.io/package/convert-array-to-csv · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
