Skip to content

Instantly share code, notes, and snippets.

@Williammer
Last active May 9, 2016 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Williammer/7e2203ed40373a9d6f96cd18ea2c4de7 to your computer and use it in GitHub Desktop.
Save Williammer/7e2203ed40373a9d6f96cd18ea2c4de7 to your computer and use it in GitHub Desktop.
Functional programming - reduce [iterate, mutate inside and outside] - sample cases
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