title: Data transformation for d3 stack
body: <p>I'm trying to reoragnise my data to run with a d3 stack generator. The code below works but generates duplicates and looks long winded.  Is there a better way to effect this transformation?</p>
  <pre><code>   From: data  = {code: &quot;sjp&quot;, year: 2020, credit: 12},
      {code: &quot;sjp&quot;, year: 2019, credit: 18},
      {code: &quot;sjp&quot;, year: 2018, credit: 10},
      {code: &quot;anp&quot;, year: 2020, credit: 16},
      {code: &quot;anp&quot;, year: 2019, credit: 14},
      {code: &quot;anp&quot;, year: 2018, credit: 12}
  
  
    To: data = {code: &quot;sjp&quot; , y2020: 12, y2019: 18, y2018: 10},
     {code: &quot;anp&quot; , y2020: 16, y2019: 14, y2018: 12}
  
  
  keyz = d3.map(d, function(s){return(s.code)})
    
  keyz.forEach(function (d) {
    var item = d.filter(function (e) { return e.code === d})
      
    for (var i = 0; i &lt; item.length; i++) {
                 switch(d[i].year) {
                         case &quot;2020&quot;:              
                             y2020 = item[i].credit
                             break;
                         case &quot;2019&quot;:                
                             y2019 = item[i].credit
                             break;
                         case &quot;2018&quot;:          
                             y2018 = item[i].credit 
                 }                     
       }                                          
    
       Newarray.push({&quot;code&quot;: d, &quot;y2020&quot;: y2020,&quot;y2019&quot;:y2019, &quot;y2018&quot;: y2018, &quot;y2017&quot;:y2017, &quot;y2016&quot;: y2016, &quot;y2015&quot;:y2015})
  </code></pre>