Trying to write more functional Javascript with the underscore library. Any thoughts on how to improve this to be more functional than imperative? Also, any way to do this without mapObject
?
The first of each pair is a "bucket" and the second is a value, I'd like to iterate over the information and get a unique list of what is in each bucket.
var a = [
['A',1],
['A',1],
['A',1],
['A',2],
['B',1],
['B',2],
['B',2],
['B',4],
['C',6],
['D',5]
];
//result should be:
// {
// A: [1,2],
// B: [1,2,4],
// C: [6],
// D: [5]
// }
_.chain(a)
.groupBy(function (pair) {
var group = pair[0];
return group;
})
.mapObject(function (val, key) {
var results = _.chain(val)
.map(function (pair) {
return pair[1]
})
.uniq()
.value();
return results;
})
.value();