Skip to content

Instantly share code, notes, and snippets.

@xandout
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xandout/702d678db43ce745bf5f to your computer and use it in GitHub Desktop.
Save xandout/702d678db43ce745bf5f to your computer and use it in GitHub Desktop.
JavaScript CSV to Array of Objects
function csv2objArr(data, strip, callback){
(strip instanceof Function) ? (callback = strip, strip = 0) : (strip = strip || 0); //Did the user specify strip?
data = data.split('\n' || '\r');
if(strip){
for(var elem in data){//loop to convert "val, val, val" to ["val", " val", " val"]
var tmpArr = data[elem].split(',');
for(var tmpElem in tmpArr){//loop to convert ["val", " val", " val"] to ["val", "val", "val"]
tmpArr[tmpElem] = tmpArr[tmpElem].trim();
}
data[elem] = tmpArr.join(); //convert ["val", "val", "val"] to "val,val,val"
}
}
var arrOfObjs = [];
//build object keys
var keys = data[0].split(',');
//Iterate through data, creating a new Object and inserting obj into arrOfObjs
for(var i = 1; i < data.length; i++ ){ //We start at 1 here so that we don't get {key:key}. We want {key:val}
var tmp = data[i].split(',');
var obj = {};
for (var ii = 0; ii < tmp.length; ii++){
obj[keys[ii]] = tmp[ii];
}
arrOfObjs.push(obj);
}
callback(arrOfObjs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment