Skip to content

Instantly share code, notes, and snippets.

@oieioi
Created April 14, 2015 01:50
Show Gist options
  • Save oieioi/dbd5f7c649f6e031dff8 to your computer and use it in GitHub Desktop.
Save oieioi/dbd5f7c649f6e031dff8 to your computer and use it in GitHub Desktop.
parse csv to JSON
// parseCsv('f1,2\nf2,3\nf3,4\n', ['name', 'value'])
// => [{"name":"f1", "value":"2"}, {"name":"f2", "value":"3"}, {"name":"f3", "value":"4"} ]
export default function parseCsv(str, header){
var lines = str.split('\n');
return lines.map((ln) => {
var items = ln.split(',');
var ob = {};
header.forEach((it, count)=>ob[it] = items[count]);
return ob;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment