title: Create Multilevel groups from an Array of Objects
body: <p>I have following type of data</p>
  
  <pre><code>data = [{
      "grp" : ["A", "B", "C"],
      "val" : [1, 2, 3]
    }, {
      "grp" : ["A", "B", "D"],
      "val" : [2, 3, 4]
    }, {
      "grp" : ["A", "C", "C"],
      "val" : [1, 3, 5]
    }, {
      "grp" : ["B", "Y", "E"],
      "val" : [1, 3, 2]
    }
  ]
  </code></pre>
  
  <p>I want to make groups like these</p>
  
  <pre><code>groups = {
    "A": {
      "B": {
        "C": [
          [1, 2, 3],
          [2, 3, 4]
        ],
        "D": [
          [1, 2, 3],
          [2, 3, 4]
        ]
      },
      "C": {
        "C": [
          [1, 3, 5]
        ]
      }
    },
    "B": {
      "Y": {
        "E": [
          [1, 3, 2]
        ]
      }
    }
  }
  </code></pre>
  
  <p><code>grp</code> array can be have maximum of 3 elements.</p>
  
  <p>I tried using underscorejs something like below, however, I ended up creating individual groups, not nested groups.</p>
  
  <pre><code>var groups = _(data).reduce(function(memo, o) {
      _(o.groups).each(function(j) {
          memo[j] = memo[j] || [ ];
          memo[j].push(o);
      });
      return memo;
  }, { });
  </code></pre>
  
  <p>Would appreciate any idea in the right direction.</p>