Skip to content

Instantly share code, notes, and snippets.

@LayneSmith
Created July 7, 2017 20:40
Show Gist options
  • Save LayneSmith/2ea73646220a7eade4dc8b5f0a74438c to your computer and use it in GitHub Desktop.
Save LayneSmith/2ea73646220a7eade4dc8b5f0a74438c to your computer and use it in GitHub Desktop.
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