Skip to content

Instantly share code, notes, and snippets.

@selfup
Created September 16, 2018 02:08
Show Gist options
  • Save selfup/60876bc7760d3771fbcd3237106b9840 to your computer and use it in GitHub Desktop.
Save selfup/60876bc7760d3771fbcd3237106b9840 to your computer and use it in GitHub Desktop.
CSV String to Array of Objects
const fileString = "title,body\nhello,wow\nwow,ok";
const csv = fileString.split('\n');
console.log(csv);
const headers = csv[0].split(',');
const rows = csv.splice(1);
const result = [];
rows.map((row) => {
const rowObject = {};
row
.split(',')
.forEach((rowValue, i) => rowObject[headers[i]] = rowValue);
result.push(rowObject);
});
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment