Here is an array I need to work with:
0: {productid: "001", containersize: "20", ContCount: 10}
1: {productid: "002", containersize: "20", ContCount: 9}
2: {productid: "001", containersize: "40", ContCount: 4}
3: {productid: "001", containersize: "20", ContCount: 20}
4: {productid: "003", containersize: "20", ContCount: 18}
5: {productid: "001", containersize: "40", ContCount: 7}
6: {productid: "003", containersize: "40", ContCount: 25}
I aim to transform this data into a new array like so:
0: {productid: "001", containersize20: 30, containersize40: 11, total: 41}
1: {productid: "002", containersize20: 9, containersize40: 0, total: 9}
2: {productid: "003", containersize20: 18, containersize40: 25, total: 43}
The purpose is to sum up the counts of containers based on product type and container size.
Initially, I used the following code snippet:
var group_to_values = data.reduce(function (obj, item) {
obj[item.productid] = obj[item.productid] || [];
obj[item.productid].push(item.ContCount);
return obj;
}, {});
var groups = Object.keys(group_to_values).map(function (key) {
return {productid: key, ContCount: group_to_values[key]};
});
At this stage, I am facing difficulty in proceeding further with the transformation process.