Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
Last active July 21, 2023 13:53
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save JeffJacobson/2770509 to your computer and use it in GitHub Desktop.
Save JeffJacobson/2770509 to your computer and use it in GitHub Desktop.
JavaScript object to CSV
/**
* Converts a value to a string appropriate for entry into a CSV table. E.g., a string value will be surrounded by quotes.
* @param {string|number|object} theValue
* @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
*/
function toCsvValue(theValue, sDelimiter) {
var t = typeof (theValue), output;
if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
sDelimiter = '"';
}
if (t === "undefined" || t === null) {
output = "";
} else if (t === "string") {
output = sDelimiter + theValue + sDelimiter;
} else {
output = String(theValue);
}
return output;
}
/**
* Converts an array of objects (with identical schemas) into a CSV table.
* @param {Array} objArray An array of objects. Each object in the array must have the same property list.
* @param {string} sDelimiter The string delimiter. Defaults to a double quote (") if omitted.
* @param {string} cDelimiter The column delimiter. Defaults to a comma (,) if omitted.
* @return {string} The CSV equivalent of objArray.
*/
function toCsv(objArray, sDelimiter, cDelimiter) {
var i, l, names = [], name, value, obj, row, output = "", n, nl;
// Initialize default parameters.
if (typeof (sDelimiter) === "undefined" || sDelimiter === null) {
sDelimiter = '"';
}
if (typeof (cDelimiter) === "undefined" || cDelimiter === null) {
cDelimiter = ",";
}
for (i = 0, l = objArray.length; i < l; i += 1) {
// Get the names of the properties.
obj = objArray[i];
row = "";
if (i === 0) {
// Loop through the names
for (name in obj) {
if (obj.hasOwnProperty(name)) {
names.push(name);
row += [sDelimiter, name, sDelimiter, cDelimiter].join("");
}
}
row = row.substring(0, row.length - 1);
output += row;
}
output += "\n";
row = "";
for (n = 0, nl = names.length; n < nl; n += 1) {
name = names[n];
value = obj[name];
if (n > 0) {
row += ","
}
row += toCsvValue(value, '"');
}
output += row;
}
return output;
}
@JeffJacobson
Copy link
Author

Licensed under The MIT License

@Yaffle
Copy link

Yaffle commented Jan 9, 2014

on line 64 row += cDelimiter; should be

@Yaffle
Copy link

Yaffle commented Jan 9, 2014

also, you should double quotes in the value:
theValue.replace(/"/g, '""')
and disallow to use any other "cDelimeter"s

@select
Copy link

select commented Aug 13, 2018

Hi there, thanks for the code, I had a look at your code but a bit different requirements. Here is my solution that deals with arrays of non uniform objects https://gist.github.com/select/0e2aa49b98ea81db7c615e6560497c41

@bhelm
Copy link

bhelm commented Jul 4, 2019

This code is does not produce correct csvs. It does not use the column delimiter (only for the first row) and does not escape the value if it contains the sDelimiter. Here is my fixed version with hardcoded sdelimiter to ": https://gist.github.com/bhelm/0c3800359c73e2797e3fc545b1676aed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment