# convert-to-csv

> This package provides a utility function to convert an array of objects into a CSV format, suitable for exporting data from JavaScript applications.

Latest version **1.0.6** (published 2024-07-05) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2024-07-05 |
| First published | 2024-02-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Ashutosh Maurya |
| Maintainers | ashutoshmaurya |
| Keywords | convert-to-csv, csv, array-to-csv, export, export-to-csv |

## Links

- npm: https://www.npmjs.com/package/convert-to-csv
- Repository: https://github.com/ashutoshofficial25/convert-to-csv
- Homepage: https://github.com/ashutoshofficial25/convert-to-csv#readme
- Issues: https://github.com/ashutoshofficial25/convert-to-csv/issues
- npm.io page: https://npm.io/package/convert-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

- 1.0.6 (latest) — 2024-07-05
- 1.0.5 — 2024-02-19
- 1.0.4 — 2024-02-19
- 1.0.3 — 2024-02-19
- 1.0.2 — 2024-02-19
- 1.0.1 — 2024-02-19
- 1.0.0 — 2024-02-19

## README

# Convert to CSV

This package provides a utility function to convert an array of objects into a CSV format, suitable for exporting data from JavaScript applications.

## Installation

You can install this package via npm:

```bash
npm install convert-to-csv
```

## Usage

First, import the convertToCSV function from the 'convert-to-csv' package:

```javascript
import { convertToCSV } from 'convert-to-csv';
```

### Example :

Let's say you have an array of user objects like this:

```javascript
const users = [
  {
    name: 'Ashutosh',
    email: 'example@example.com',
    address: { city: 'Lucknow', postalCode: 226010, state: 'UP' },
  },
  {
    name: 'Yuvaraj',
    email: 'example@gmail.com',
    address: { city: 'Varanasi', postalCode: 226016, state: 'UP' },
  },
];
```

### Return CSV only (For Nodejs/ backend projects.)

To get this data into a CSV format, you can use the convertToCSV function:

```javascript
const csv = convertToCSV({ data: users, isDownload: false });
or;
const csv = convertToCSV({ data: users });
```

The downloaded CSV file will have the specified custom headers.

To download this data into a CSV file, you can use the convertToCSV function download to true for frontend projects:

```javascript
convertToCSV({ data: users, isDownload: true });
```

### Download CSV with a Custom File Name

If you want to specify a custom file name for the downloaded CSV file, you can pass it as an option to the convertToCSV function:

```javascript
convertToCSV({data : users, name="mycsvfile" })
```

This will download the CSV file with the name "mycsvfile.csv".

### Download or get CSV with a Custom Headers

You can also provide custom headers for the CSV file by passing an array of strings to the `header` option. Here's how you can use it:

```javascript
const customHeaders = ['Name', 'Email', 'City', 'Postal Code', 'State'];

convertToCSV({ data: users, header: customHeaders, name: 'custom_csv' });
```

The downloaded CSV file will have the specified custom headers.

### Explanation

The `convertToCSV` function takes an object as its parameter with the following properties:

- `data`: An array of objects that you want to convert to CSV format.
- `name` (optional): A string representing the custom file name for the downloaded CSV file. If not provided, it defaults to "download.csv".
- `header` (optional): An array of strings representing custom headers for the CSV file. If provided, these headers will be used instead of deriving them from the keys of the objects in the array.

When you call `convertToCSV`, it converts the array of objects into CSV format and initiates a download of the CSV file.

The CSV file will have headers either derived from the keys of the objects in the array or from the provided `header` array, and each object's properties will be listed as values in the corresponding columns.

If a custom file name is provided, the downloaded CSV file will have that name; otherwise, it will default to "download.csv".

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