Problem Statement :
I have an array of objects with month , sales & orders as attributes. now, I want to create a function like following. which can provide result based on the argument.
function aggregate(json_data , yearly)
{
// This function could return an array of objects like following
// [ { Month : 2014 , Sales : x , Amount : x},
// { Month : 2015 , Sales : x , Amount : x }
// ]
};
If the user pass argument as quarter , then data should aggregate quarterly
function aggregate(json_data , quarterly)
{
// This function could return an array of objects
// [ { Month : 2014Q1 , Sales : x , Amount : x},
// { Month : 2014Q2 , Sales : x , Amount : x }
// ............
// ............
// ]
}
Sample Data
var data = [
{
Month: 201401,
Sales: 15,
Orders: 4
},
{
Month: 201402,
Sales: 12,
Orders: 3
},
{
Month: 201403,
Sales : 16,
Orders: 5
},
{
Month: 201404,
Sales: 12,
Orders: 2
},
{
Month: 201405,
Sales: 12,
Orders: 4
},
{
Month: 201406,
Sales: 10,
Orders: 3
},
{
Month: 201407,
Sales: 15,
Orders: 2
},
{
Month: 201408,
Sales: 14,
Orders: 3
},
{
Month: 201409,
Sales: 13,
Orders: 6
},
{
Month: 201410,
Sales: 13,
Orders: 5
},
{
Month: 201411,
Sales: 12,
Orders: 2
},
{
Month: 201412,
Sales: 11,
Orders: 4
},
{
Month: 201501,
Sales: 15,
Orders: 4
},
{
Month: 201502,
Sales: 12,
Orders: 6
},
{
Month: 201503,
Sales : 6,
Orders: 5
},
{
Month: 201504,
Sales: 10,
Orders: 11
},
{
Month: 201505,
Sales: 10,
Orders: 2
},
{
Month: 201506,
Sales: 10,
Orders: 3
},
{
Month: 201507,
Sales: 10,
Orders: 1
},
{
Month: 201508,
Sales: 10,
Orders: 4
},
{
Month: 201509,
Sales: 10,
Orders: 2
},
{
Month: 201510,
Sales: 10,
Orders: 3
},
{
Month: 201511,
Sales: 10,
Orders: 2
},
{
Month: 201512,
Sales: 10,
Orders: 1
}
];
Please advice, If there is any other suggestions either to adapt the question.
Someone has suggested that it is a duplicated question. However, I am working with date object. ( Please check the duplicate link before suggesting)
You could use a hash table and use a key based on the wanted yearly or quarterly aggregation of the data.
This proposal uses Month
as string.
function aggregate(array, quarterly) { var hash = Object.create(null), result = []; data.forEach(function (o) { var [period, month] = o.Month.toString().split(/(?=..$)/); if (quarterly) { period += 'Q' + Math.floor((+month + 2) / 3); } if (!hash[period]) { hash[period] = { period, Sales: 0, Orders: 0 }; result.push(hash[period]); } hash[period].Sales += o.Sales; hash[period].Orders += o.Orders; }); return result; } var data = [{ Month: 201401, Sales: 15, Orders: 4 }, { Month: 201402, Sales: 12, Orders: 3 }, { Month: 201403, Sales: 16, Orders: 5 }, { Month: 201404, Sales: 12, Orders: 2 }, { Month: 201405, Sales: 12, Orders: 4 }, { Month: 201406, Sales: 10, Orders: 3 }, { Month: 201407, Sales: 15, Orders: 2 }, { Month: 201408, Sales: 14, Orders: 3 }, { Month: 201409, Sales: 13, Orders: 6 }, { Month: 201410, Sales: 13, Orders: 5 }, { Month: 201411, Sales: 12, Orders: 2 }, { Month: 201412, Sales: 11, Orders: 4 }, { Month: 201501, Sales: 15, Orders: 4 }, { Month: 201502, Sales: 12, Orders: 6 }, { Month: 201503, Sales: 6, Orders: 5 }, { Month: 201504, Sales: 10, Orders: 11 }, { Month: 201505, Sales: 10, Orders: 2 }, { Month: 201506, Sales: 10, Orders: 3 }, { Month: 201507, Sales: 10, Orders: 1 }, { Month: 201508, Sales: 10, Orders: 4 }, { Month: 201509, Sales: 10, Orders: 2 }, { Month: 201510, Sales: 10, Orders: 3 }, { Month: 201511, Sales: 10, Orders: 2 }, { Month: 201512, Sales: 10, Orders: 1 }]; console.log(aggregate(data)); console.log(aggregate(data, true));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Not entirely sure if it's the same problem, but I recently had to solve what seems, at least, a very similar problem. I had an array of objects with a date and a value, such as this:
statsData = [
{ x: randomDate, y: 1 },
{ x: randomDate, y: 2 },
{ x: randomDate, y: 1 },
{ x: randomDate, y: 4 },
{ x: randomDate, y: 3 },
{ x: randomDate, y: 2 },
{ x: randomDate, y: 1 },
{ x: randomDate, y: 4 },
{ x: randomDate, y: 6 },
{ x: randomDate, y: 5 },
{ x: randomDate, y: 1 }
]
And I needed to reduce it, so that all the objects with the same month are grouped together under one object that adds all the values of that month.
What I did was, first to sort them, in case they don't come chronollogically ordered:
sortData(data) {
return data.sort((a, b) => {
return +a.x - +b.x;
});
}
And then use this reduce function:
reduceByMonth(data) {
if (data) {
return data.reduce((total, current, index) => {
const month = { x: null, y: 0 };
if (index !== 0 && (total && total[total.length - 1] && total[total.length - 1].x && monthsMatch(total[total.length - 1].x, current.x))) {
total[total.length - 1].y = total[total.length - 1].y + current.y;
return total;
} else {
month.x = new Date(current.x.getFullYear(), current.x.getMonth());
month.y = month.y + current.y;
total.push(month);
return total;
}
}, []);
} else {
console.error('Cannot reduce undefined data');
return []
}
}
the function monthsMatch is simply checking if the months of two dates match:
monthsMatch(dateA, dateB) {
if (dateA.getFullYear() === dateB.getFullYear()) {
return dateA.getMonth() === dateB.getMonth();
} else {
return false;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.