|
| 1 | +# convert-array-to-csv |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/convert-array-to-csv) |
| 4 | +[](https://travis-ci.org/aichbauer/node-convert-array-to-csv) |
| 5 | +[](https://codecov.io/gh/aichbauer/node-convert-array-to-csv) |
| 6 | + |
| 7 | +> Convert an array to a csv formatted string |
| 8 | +
|
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +* [Why?](#why) |
| 12 | +* [Installation](#installation) |
| 13 | +* [Functions](#functions) |
| 14 | +* [Usage](#usage) |
| 15 | +* [License](#license) |
| 16 | + |
| 17 | +## Why? |
| 18 | + |
| 19 | +I needed a simple way to download the data from a table component in a csv format. |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```sh |
| 24 | +$ npm i convert-array-to-csv -S |
| 25 | +``` |
| 26 | + |
| 27 | +or |
| 28 | + |
| 29 | +```sh |
| 30 | +$ yarn add convert-array-to-csv |
| 31 | +``` |
| 32 | + |
| 33 | +## Functions |
| 34 | + |
| 35 | +Take a look into the [usage section](#usage) for a detailed example. |
| 36 | + |
| 37 | +### convertArrayToCSV |
| 38 | + |
| 39 | +> Note: you can also use the default export. |
| 40 | +
|
| 41 | +This function converts an array of objects, or an array of arrays into an csv formatted string. |
| 42 | + |
| 43 | +#### Syntax |
| 44 | + |
| 45 | +Returns a new string. |
| 46 | + |
| 47 | +```js |
| 48 | +const csv = convertArrayToCSV(data, options); |
| 49 | +``` |
| 50 | + |
| 51 | +##### Parameters |
| 52 | + |
| 53 | +* data: an array of arrays or an array of objects |
| 54 | +* options: a object |
| 55 | + * holds two keys: header and separator |
| 56 | + * **header**: and array with the name of the columns, default: `undefined` |
| 57 | + * **separator**: the character which is the separator in your csv formatted string, default: `','` |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +An example how to use it. |
| 62 | + |
| 63 | +```js |
| 64 | +const { convertArrayToCSV } = require('convert-array-to-csv'); |
| 65 | +const converter = require('convert-array-to-csv'); |
| 66 | + |
| 67 | +const header = ['number', 'first', 'last', 'handle']; |
| 68 | +const dataArrays = [ |
| 69 | + [1, 'Mark', 'Otto', '@mdo'], |
| 70 | + [2, 'Jacob', 'Thornton', '@fat'], |
| 71 | + [3, 'Larry', 'the Bird', '@twitter'], |
| 72 | +]; |
| 73 | +const dataObjects = [ |
| 74 | + { |
| 75 | + number: 1, |
| 76 | + first: 'Mark', |
| 77 | + last: 'Otto', |
| 78 | + handle: '@mdo', |
| 79 | + }, |
| 80 | + { |
| 81 | + number: 2, |
| 82 | + first: 'Jacob', |
| 83 | + last: 'Thornton', |
| 84 | + handle: '@fat', |
| 85 | + }, |
| 86 | + { |
| 87 | + number: 3, |
| 88 | + first: 'Larry', |
| 89 | + last: 'the Bird', |
| 90 | + handle: '@twitter', |
| 91 | + }, |
| 92 | +]; |
| 93 | + |
| 94 | +/* |
| 95 | + const csvFromArrayOfObjects = 'number,first,last,handle\n1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,the Bird,@twitter\n'; |
| 96 | +*/ |
| 97 | +const csvFromArrayOfObjects = convertArrayToCSV(dataObjects); |
| 98 | + |
| 99 | +/* |
| 100 | + const csvFromArrayOfArrays = 'number;first;last;handle\n1;Mark;Otto;@mdo\n2;Jacob;Thornton;@fat\n3;Larry;the Bird;@twitter\n'; |
| 101 | +*/ |
| 102 | +const csvFromArrayOfArrays = convertArrayToCSV(dataArrays, { |
| 103 | + header, |
| 104 | + separator: ';' |
| 105 | +}); |
| 106 | +``` |
| 107 | + |
| 108 | +## License |
| 109 | + |
| 110 | +MIT © Lukas Aichbauer |
0 commit comments