So I have this array:
var period = [{"total":852, "date":"2016-03"}, {"total":963, "date":"2016-03"},{"total":789,"date":"2016-02"},{"total":456,"date":"2016-04"},{"total":123,"date":"2016-01"},{"total":723,"date":"2016-01"}];
I need to display "total" data grouped by month. Which means I have to sum "total" amount on months that are repeated on the array (2016-03, 2016-01). To find the solution I need to understand why this
for ( var i = 0; i < period.length; i++ ){
if (periodB.indexOf(period[i].date) == -1){
periodB.push(period[i].date);
}
Returns this:
["2016-03", "2016-02", "2016-04", "2016-01"]
While this:
for ( var i = 0; i < period.length; i++ ){
if (periodB.indexOf(period[i].date) == -1){
periodB.push({"date": period[i].date, "total": period[i].total});
}
}
Is returning this:
[{date: "2016-03",total: 1704}, {date: "2016-03", total: 1926}, {date:"2016-02", total: 1578},{date: "2016-04",total: 912}, {date: "2016-01",total: 246}, {date: "2016-01", total: 1446 }]
On the first case repeated "dates" are not being pushed on to periodB array, but then on the second case they are.