Skip to content

Instantly share code, notes, and snippets.

@GuilhermeRossato
Created April 18, 2022 20:44
Show Gist options
  • Save GuilhermeRossato/60623f6d9436952f6c32527fb29c0720 to your computer and use it in GitHub Desktop.
Save GuilhermeRossato/60623f6d9436952f6c32527fb29c0720 to your computer and use it in GitHub Desktop.
Transform JS Array of CSV string
/**
* Transform an array of objects in javascript into a single csv string with the header as the first line
*
* Works on any javascript version above ES6 (2015)
*
* @param array {any[]}
* @param columnDelimiter {string}
* @param quoteChar {string}
* @param newline {string}
* @returns {string}
*/
function arrayToCsvString(array, columnDelimiter = ',', quoteChar = '"', newline = '\n') {
const columnNameList = [];
for (const obj of array) {
if (typeof obj !== 'object') {
continue;
}
for (const property in obj) {
if (columnNameList.indexOf(property) === -1) {
columnNameList.push(property);
}
}
}
function transformColumnValueToCsvColumn(value) {
if (value === null || value === undefined) {
return '';
}
if (typeof value !== 'string') {
value = value.toString();
}
if (value.includes(columnDelimiter) || value.includes(quoteChar) || value.includes(newline)) {
return quoteChar + value.split(quoteChar).join(quoteChar + quoteChar) + quoteChar;
}
return value;
}
const lines = [
columnNameList.map(columnName => transformColumnValueToCsvColumn(columnName)).join(columnDelimiter)
];
for (const obj of array) {
if (typeof obj === 'object') {
const line = [];
for (const property of columnNameList) {
line.push(transformColumnValueToCsvColumn(obj[property]));
}
lines.push(line.join(columnDelimiter));
}
}
return lines.join(newline);
}
/**
* Transform a array of objects into a single csv string with the header as the first line
* @param array
* @param columnDelimiter
* @param quoteChar
* @param newline
* @returns {string}
*/
function arrayToCsvString(array: any[], columnDelimiter = ',', quoteChar = '"', newline = '\n'): string {
const columnNameList: string[] = [];
for (const obj of array) {
if (typeof obj !== 'object') {
continue;
}
for (const property in obj) {
if (!columnNameList.includes(property)) {
columnNameList.push(property);
}
}
}
function transformColumnValueToCsvColumn(value: any) {
if (value === null || value === undefined) {
return '';
}
if (typeof value !== 'string') {
value = value.toString();
}
if (value.includes(columnDelimiter) || value.includes(quoteChar) || value.includes(newline)) {
return quoteChar + value.split(quoteChar).join(quoteChar + quoteChar) + quoteChar;
}
return value;
}
const lines: string[] = [
columnNameList.map(columnName => transformColumnValueToCsvColumn(columnName)).join(columnDelimiter)
];
for (const obj of array) {
if (typeof obj === 'object') {
const line = [];
for (const property of columnNameList) {
line.push(transformColumnValueToCsvColumn(obj[property]));
}
lines.push(line.join(columnDelimiter));
}
}
return lines.join(newline);
}

Example of usage:

const str = arrayToCsvString([{a: 1, b: 2}, {b: 3, c: 4}]);
console.log(str);

Returns the string:

a,b,c
1,2,
,3,4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment