Last active
May 16, 2018 09:49
-
-
Save izaacdb/6dd363e81b774e58b178f1f294d0bc7c to your computer and use it in GitHub Desktop.
CSV to JSON in ES6
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
export const csvToJson = (csv: string) => { | |
const brands = csv.split('\n') || 'error'; | |
const titles = brands.shift().split(','); //takes first row | |
const json = brands | |
.map((b: any) => { | |
const brand = b.split(','); | |
return brand | |
.map( | |
(x: any, i: any) => { | |
return { [titles[i]]: x }; | |
} | |
) | |
.reduce( | |
(acc: any, value: any) => { | |
return Object.assign(acc, value); | |
}, | |
{} | |
); | |
}); | |
console.log(json); | |
return json; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment