Create New Array from Existing Array Values With Unique and Sum
<p>For some reason I'm stumbling to get the result I need with Javascript arrays. </p>

<p>I have rows of data which contain day, and several timestamps per row. What I need to do is transform that data to an array for chart.js, but merging rows by week, and summing the hours (from the timestamps). Below is where I am creating a new array from the query result, but am struggling to get it how I need it.</p>

<p>Here's where I create the array (with processing):</p>

<pre><code>function chartData(queryrowlist){
  var weeks = [];
  angular.forEach(queryrowlist, function(value, key) 
  {
    weeks.push(moment(value.date).format('w'),getOnHours(value),getOffHours(value))
  });
  // create chart array ...
  return chart;
}
</code></pre>

<p>The weeks array ends up like this:</p>

<pre><code>["23",28800000,3600000,"23",30300000,2700000,"24",35400000,3300000,"24",30000000,3300000]
</code></pre>

<p>Which is Week Number, OnHours, OffHours</p>

<p>The Week Number will be used as the Label, and the Hours as the On/Off Data in angular-chart/chart.js. The problem is all of the Week Numbers have to be Unique, and all of the Hours Summed up for each week.</p>

<p>So, I will need to output an array like this:</p>

<pre><code>chart['weeks']=["23","24"]
chart['hours']=[[59100000,6300000],[65400000,6300000]]
</code></pre>

<p>This is probably simple, and it's just me hitting a thought wall... but if anyone has a solution, I'd be grateful. </p>

<p>Thank you in advance.</p>

<p><strong>Update:</strong> </p>

<p>For anyone who might refer to this, the resulting array is incorrect for chart.js. Answers below are correct... as asked, I just asked the wrong thing and only realized after. Feeling like a fool :/</p>

<p>Chart.js want's all the line values in the same array, so instead of above example which is:</p>

<pre><code>chart['weeks']=["23","24"]
chart['hours']=[[onhours,offhours],[onhours,offhours]]
</code></pre>

<p>It should be:</p>

<pre><code>chart['weeks']=["23","24"]
chart['hours']=[[onhours,onhours],[offhours,offhours]]
</code></pre>

<p><strong>Update 2</strong></p>

<p>To add further clarification:</p>

<p>For each label, there should be a value. If you have 3 labels, you should have 3 values. In my case, there's 2 sets of value arrays, and that's because I have 2 series (2 lines), 1 for On Hours, 1 for Off Hours. So here's a complete list of arrays.</p>

<pre><code>chart['labels']=["23","24","25"] // strings
chart['data']=[[onhours,onhours,onhours],[offhours,offhours,offhours]] // integers
chart['series']=['On Hours','Off Hours'] // strings
</code></pre>

<p>If I only needed 1 line:</p>

<pre><code>chart['labels']=["23","24","25","26"] // strings
chart['data']=[[onhours,onhours,onhours,onhours]] // integers, notice [ [  ] ]
chart['series']=['On Hours'] // strings
</code></pre>

<p>Hope that helps!</p>