Export JavaScript Data to CSV Format – Convert to CSV

Category: Javascript | February 25, 2024
Author:ashutoshofficial25
Views Total:23 views
Official Page:Go to website
Last Update:February 25, 2024
License:MIT

Preview:

Export JavaScript Data to CSV Format – Convert to CSV

Description:

Convert to CSV is a tiny JavaScript utility that enables users to convert JavaScript arrays of objects into a CSV file ready for download.

You can use the library for exporting data from many front-end apps and sites built with JavaScript. For example, you could use it to download a list of products, user data, form submissions, or any other array-based data.

How to use it:

1. Install and import the Convert to CSV into your project.

# NPM
$ npm install convert-to-csv
import { convertToCSV } from 'convert-to-csv';

2. The convertToCSV() function accepts an array of objects as the input data. It loops through the objects and extracts the keys as the CSV headers. Then it loops through the array again to output the object values into the corresponding columns under each header.

const languages = [
  {
    name: 'JavaScript',
    paradigm: 'Interpreted, Object-Oriented, Event-Driven',
    applications:
      'Web development, mobile apps, game development, server-side scripting',
    popularity: 'Very high',
  },
  {
    name: 'Python',
    paradigm: 'Interpreted, Object-Oriented, Imperative',
    applications: 'Web development, data science, machine learning, automation',
    popularity: 'High',
  },
  // ...
];
convertToCSV({ 
  data: languages
});

3. The library also allows customizing the filename and headers. You can override the auto-generated headers with your own if needed. And you can specify a file name instead of just “download.csv”. This gives you more control over the output.

convertToCSV({ 
  data: languages, 
  header: ['Custom', 'Headers', 'Here'], 
  name: 'popular-languages' 
});

You Might Be Interested In:


Leave a Reply