Skip to content

Instantly share code, notes, and snippets.

@iceener
Created May 13, 2020 21:24
Show Gist options
  • Save iceener/c9c54e8a00edf058e81df2f2418625e4 to your computer and use it in GitHub Desktop.
Save iceener/c9c54e8a00edf058e81df2f2418625e4 to your computer and use it in GitHub Desktop.
// CSV to JSON
const CSVToJSON = (data, delimiter = ',') => {
const titles = data.slice(0, data.indexOf('\n')).split(delimiter);
return data
.slice(data.indexOf('\n') + 1)
.split('\n')
.map(v => {
const values = v.split(delimiter);
return titles.reduce((obj, title, index) => ((obj[title] = values[index]), obj), {});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment