Created
July 7, 2017 20:40
-
-
Save LayneSmith/2ea73646220a7eade4dc8b5f0a74438c to your computer and use it in GitHub Desktop.
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
const csv = `transit_option,working_poor,working_nonpoor | |
Overall,20,20 | |
Transit,60,40 | |
Car,20,25 | |
Bike,15,10 | |
Walking,10,10`; | |
function convertCSVstring(csv){ | |
const lines = csv.split("\n"); | |
const result = []; | |
const headers=lines[0].split(","); | |
lines.forEach((d) => { | |
const obj = {}; | |
const currentline = d.split(","); | |
currentline.forEach((d, i) => { | |
if ( !isNaN(currentline[i]) ){ | |
d = +currentline[i]; | |
} else { | |
d = currentline[i]; | |
} | |
obj[headers[i]] = d; | |
}) | |
result.push(obj); | |
return result; | |
}); | |
result.shift(); | |
console.table(result); | |
} | |
const data = convertCSVstring(csv); | |
console.log(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment