Last active
December 31, 2015 13:59
-
-
Save nolanamy/7996826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function JSON2CSV(objArray) { | |
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray; | |
var str = ''; | |
var line = ''; | |
if ($("#labels").is(':checked')) { | |
var head = array[0]; | |
if ($("#quote").is(':checked')) { | |
for (var index in array[0]) { | |
var value = index + ""; | |
line += '"' + value.replace(/"/g, '""') + '",'; | |
} | |
} else { | |
for (var index in array[0]) { | |
line += index + ','; | |
} | |
} | |
line = line.slice(0, -1); | |
str += line + '\r\n'; | |
} | |
for (var i = 0; i < array.length; i++) { | |
var line = ''; | |
if ($("#quote").is(':checked')) { | |
for (var index in array[i]) { | |
var value = array[i][index] + ""; | |
line += '"' + value.replace(/"/g, '""') + '",'; | |
} | |
} else { | |
for (var index in array[i]) { | |
line += array[i][index] + ','; | |
} | |
} | |
line = line.slice(0, -1); | |
str += line + '\r\n'; | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/sturtevant/vUnF9/