title: Combined use of map and group for transforming array
body: <p>I need some help for combining map and group to transform my data:</p>
  
  <pre><code>const source = [
    { _id: { hour: 18, score: 1 }, value: 45 },
    { _id: { hour: 18, score: 5 }, value: 23 },
    { _id: { hour: 18, score: 4 }, value: 34 },
    { _id: { hour: 20, score: 1 }, value: 47 },
    { _id: { hour: 20, score: 5 }, value: 25 },
    { _id: { hour: 20, score: 4 }, value: 36 },
    { _id: { hour: 19, score: 1 }, value: 46 },
    { _id: { hour: 19, score: 5 }, value: 24 },
    { _id: { hour: 19, score: 4 }, value: 35 }
  ]
  </code></pre>
  
  <p>This is how the result should look like:</p>
  
  <pre><code>{
    label: [18, 19, 20], // reordered hour values
    datasets: [
      { // each sorted score should get the values in an array sorted by hour
        data: [45, 46, 47],
        score: 1
      },
      {
        data: [34, 35, 36],
        score: 4
      },
      {
        data: [23, 24, 25],
        score: 5
      }
    ]
  }
  </code></pre>
  
  <p>So for the label I'm just doing </p>
  
  <pre><code>const label = data.map(d =&gt; d._id.hour)
  </code></pre>
  
  <p>How do I get the dataset <code>data</code> for <em>each</em> score? I would think of a loop:</p>
  
  <pre><code>label.forEach(function(element) {
    datasets.push({
      data: source.filter(d =&gt; d._id.hour === element).map(d =&gt; d.value),
      score: // ??
    })
  })
  </code></pre>
  
  <p>But this is not working, as I do get all datasets of an hour, but I need some grouped data for the score.
  And also with my attempt there is not sorting at all. Sorting for hour and score is needed.</p>