Created
December 7, 2017 18:04
-
-
Save rogerwelin/df662791bcf0d8db04b29aa508ebf958 to your computer and use it in GitHub Desktop.
fun-fun-function-reduce-4
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
/* file we're processing | |
random name,waffle iron,80,2 | |
random name,blender,200,1 | |
random name,knife,10,4 | |
hulk hogan,waffle iron,80,1 | |
hulk hogan,blender,180,3 | |
hulk hogan,pot,90,3 | |
*/ | |
const fs = require('fs') | |
// mission: parse text-file and turn it into a structured dict/list | |
var output = fs.readFileSync('4-data.txt', '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(JSON.stringify(output, null, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment