Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Hey, I needed JS CSV parser few days ago. Here is my parser, I bet it is 100 times faster than yours :)

function parseCSV(str) { var obj = {}; var lines = str.split("\n"); var attrs = lines[0].split(","); for(var i=0; i<attrs.length; i++) obj[attrs[i]] = []; for(var i=1; i<lines.length-1; i++) { var line = lines[i].split(","); for(var j=0; j<line.length; j++) obj[attrs[j]].push(line[j]); } return obj; }



Congratulations, this code failed to properly parse my CSV at least 100 times faster than OP's :)


Looks like you've missed values enclosed in quotes that might contain new line characters or commas -- very important for things like address fields.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: