Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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();
share|improve this question
1  
Please state only the code's specific purpose in the title. –  Jamal Apr 22 at 21:59

1 Answer 1

up vote 1 down vote accepted

Well you can avoid a few variable assignments by returning immediately, also by using some details of the Underscore.js API, that is by specifying the used indexes for groupBy/map directly.

_.chain(a)
 .groupBy(0)
 .mapObject(function (val, key) {
    return _.chain(val).map(1).uniq().value();
 })
 .value();

I doubt there's a good way to avoid mapObject, after all, that's what it's for.

With 1.7 you could also write that even less verbose:

_.chain(a)
 .groupBy(0)
 .mapObject((val, key) => _.chain(val).map(1).uniq().value())
 .value();

Looks pretty functional to me.

share|improve this answer
1  
Thanks for the tips! I'm forced to use an older version of underscore that didn't have mapObject but I worked around it by chaining map and then object. –  dave Apr 23 at 17:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.