Last active
February 21, 2018 01:03
-
-
Save SakoMe/f64d4a2c22138635e566800a940da9ad to your computer and use it in GitHub Desktop.
Use of advanced reduce method
This file contains 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
Mark Williams | waffle iron | 80 | 2 | |
---|---|---|---|---|
Mark Williams | blender | 200 | 1 | |
Mark Williams | knife | 10 | 4 | |
Nikita Smith | waffle iron | 80 | 1 | |
Nikita Smith | knife | 10 | 2 | |
Nikita Smith | pot | 20 | 3 |
This file contains 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 fs = require('fs'); | |
let output = fs.readFileSync('data.csv', 'utf8') | |
.trim() | |
.split('\n') | |
.map(line => line.split(',')) | |
.reduce((customers, line) => { | |
customers[line[0]] = customers[line[0]] || [] | |
customers[line[0]].push({ | |
name: line[1], | |
price: line[2], | |
quantity: line[3] | |
}) | |
return customers | |
}, {}) | |
console.log('output', JSON.stringify(output, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment