Created
September 16, 2018 02:08
-
-
Save selfup/60876bc7760d3771fbcd3237106b9840 to your computer and use it in GitHub Desktop.
CSV String to Array of Objects
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 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