-
-
Save lucasdu4rte/d95dc7fd12d2ad8873364ed59dee019b to your computer and use it in GitHub Desktop.
CSV to JSON Conversion in JavaScript
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
var csvJSON = function(csv){ | |
var lines = csv.split("\n") | |
var result = [] | |
var headers = lines[0].split(",") | |
lines.map(function(line, indexLine){ | |
if (indexLine < 1) return // Jump header line | |
var obj = {} | |
var currentline = line.split(",") | |
headers.map(function(header, indexHeader){ | |
obj[header] = currentline[indexHeader] | |
}) | |
result.push(obj) | |
}) | |
result.pop() // remove the last item because undefined values | |
return result //JavaScript object | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment