title: Underscore.js convert object to array
body: <p>I'm relatively new to underscore.js and I have an object coming in from a REST service which looks like this (I have typed it out by hand and assigned it to a var here):</p>
  
  <pre><code>var production = [
      {key: ["wind", "1"], value: 5},
      {key: ["wind", "2"], value: 9},
      {key: ["wind", "3"], value: 11},
      {key: ["wind", "4"], value: 7},
      {key: ["solar", "1"], value: 1},
      {key: ["solar", "2"], value: 1},
      {key: ["solar", "3"], value: 2},
      {key: ["solar", "4"], value: 3},
      {key: ["oil", "1"], value: 15},
      {key: ["oil", "2"], value: 16},
      {key: ["oil", "3"], value: 22},
      {key: ["oil", "4"], value: 23},
  ];
  </code></pre>
  
  <p>Then further down, I have some code that parses this object and creates arrays for the items like so:</p>
  
  <pre><code>var orderedProduction = _.chain(production)
        .groupBy(function (entity) {
              return entity.key[0];
        })
        .map(function (values) { 
              return _.map(values, function (entity2) {
                    return entity2.value;
              });
        })
        .value();
  </code></pre>
  
  <p>This gives the following results:</p>
  
  <pre><code>orderedProduction  = [
    [5, 9, 11, 7],
    [1, 1, 2, 3],
    [15, 16, 22, 23]
  ]
  </code></pre>
  
  <p>Which loses the keys (wind/solar/oil). And is used to draw a graph. I'm further able to check if some of these arrays meet a certain threshold like so:</p>
  
  <pre><code>var threshold = _.map(orderedProduction  , function(arr) {
      return _.max(arr) &gt; 10;
  });
  </code></pre>
  
  <p>My requirements have now changed and I now need to filter out these arrays by its total sum, while retaining the key.</p>
  
  <p>What I would like to end up with an object like so:</p>
  
  <pre><code>orderedProduction  = {
        "wind": [5, 9, 11, 7],
        "solar": [1, 1, 2, 3],
        "oil": [15, 16, 22, 23]
      }
  </code></pre>
  
  <p>It would be great if the fix included a way to sum up the values of the array and remove those that do not total up to a certain amount. (i.e. exclude solar, if it does not add up to 10).</p>
  
  <p>Here is a jsfiddle I created to test this all out on: <a href="http://jsfiddle.net/2mfjw3jk/" rel="nofollow">http://jsfiddle.net/2mfjw3jk/</a></p>
  
  <hr>
  
  <p>UPDATE:
  The solution I settled for was this:</p>
  
  <pre><code>var orderedProduction = _.chain(production)
      .groupBy(function (entity) {
          return entity.key[0];
      })
      .map(function (vals, key) {
          return [key, _.pluck(vals, 'value')]
      })
      .filter(function(arr) {
          var sum = 0;
          _.each(arr[1], function(num){
              sum += num;
          })
          return sum &gt; 10;
      })
      .object()
      .value();
  </code></pre>
  
  <p>It also filters the values by a predetermined threshold (10 in this case).</p>