Created
February 25, 2020 21:31
-
-
Save pilgreen/52c5ca9861e1941cf306f5d7c9060570 to your computer and use it in GitHub Desktop.
Simple lightweight CSV conversion to JSON.
This file contains hidden or 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
function csvToJSON(csv) { | |
const lines = csv.split('\n') | |
const result = [] | |
const headers = lines[0].split(',') | |
for (let i = 1; i < lines.length; i++) { | |
if (!lines[i]) | |
continue | |
const obj = {} | |
const currentline = lines[i].split(',') | |
for (let j = 0; j < headers.length; j++) { | |
obj[headers[j]] = currentline[j] | |
} | |
result.push(obj) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment