Last active
May 9, 2016 15:47
-
-
Save Williammer/7e2203ed40373a9d6f96cd18ea2c4de7 to your computer and use it in GitHub Desktop.
Functional programming - reduce [iterate, mutate inside and outside] - sample cases
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
var fs = require('fs') | |
var out = fs.readFileSync('src.txt', 'utf8') | |
.trim().split('\n') | |
.reduce(function (acc, cur) { | |
var curArr = cur.split(' '); | |
if (typeof acc[curArr[0]] !== 'object') { | |
acc[curArr[0]] = []; | |
} | |
acc[curArr[0]].push({ | |
name: curArr[1], | |
price: curArr[2], | |
quantity: curArr[3] | |
}) | |
return acc | |
}, {}) | |
console.log('out', JSON.stringify(out, null, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment