Created
April 14, 2015 01:50
-
-
Save oieioi/dbd5f7c649f6e031dff8 to your computer and use it in GitHub Desktop.
parse csv to JSON
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
// 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