Convert array of objects into a CSV file
Converts an array of JavaScript objects into the CSV format. You can save the CSV to file or return it as a string.
The keys in the first object of the array will be used as column names.
Any special characters in the values (such as commas) will be properly escaped.
Usage
const ObjectsToCsv = ; // Sample data - two columns, three rows:const data = code: 'CA' name: 'California' code: 'TX' name: 'Texas' code: 'NY' name: 'New York'; // If you use "await", code must be inside an asynchronous function:async { const csv = data; // Save to file: await csv; // Return the CSV file as string: console;};
Methods
There are two methods, toDisk(filename)
and toString()
.
async toDisk(filename, options)
Converts the data and saves the CSV file to disk. The filename
must include the
path as well.
The options
is an optional parameter which is an object that contains the
settings. Supported options:
append
- whether to append to the file. Default isfalse
(overwrite the file). Set totrue
to append. Column names will be added only once at the beginning of the file. If the file does not exist, it will be created.bom
- whether to add the Unicode Byte Order Mark at the beginning of the file. Default isfalse
; set totrue
to be able to view Unicode in Excel properly. Otherwise Excel will display Unicode incorrectly.allColumns
- whether to check all array items for keys to convert to columns rather than only the first. This will sort the columns alphabetically. Default isfalse
; set totrue
to check all items for potential column names.
const ObjectsToCsv = ;const sampleData = id: 1 text: 'this is a test' ; // Run asynchronously, without awaiting:sampleData; // Alternatively, you can append to the existing file:sampleData; // `allColumns: true` collects column names from all objects in the array,// instead of only using the first one. In this case the CSV file will// contain three columns:const mixedData = id: 1 name: 'California' id: 2 description: 'A long description.' ;mixedData;
async toString(header = true, allColumns = false)
Returns the CSV file as a string.
Two optional parameters are available:
header
controls whether the column names will be returned as the first row of the file. Default istrue
. Set it tofalse
to get only the data rows, without the column names.allColumns
controls whether to check every item for potential keys to process, rather than only the first item; this will sort the columns alphabetically by key name. Default isfalse
. Set it totrue
to process keys that may not be present in the first object of the array.
const ObjectsToCsv = ;const sampleData = id: 1 text: 'this is a test' ; { console;} ;
Requirements
Use Node.js version 8 or above.