I have an array of objects like this
data = [
{
Category_Name: "Furniture",
Status_Name: "Available",
Count: 1
},
{
Category_Name: "License",
Status_Name: "Available",
Count: 3
},
{
Category_Name: "Laptop",
Status_Name: "Good Condition",
Count: 1
},
{
Category_Name: "Laptop",
Status_Name: "Good Condition",
Count: 1
},
{
Category_Name: "Vehicle",
Status_Name: "Requested",
Count: 2
},
{
Category_Name: "Furniture",
Status_Name: "Good Condition",
Count: 1
}
];
I am trying to, first, group by same Category_Name
and sum count values which I made it right, and second create a new array called status
inside returned object which will count by status names (Available, Good Condition and Requested)
. So the final result will be like
[
{
Category_Name: "Furniture",
total: 2,
status: [
{
Available: 1,
Good_Condition: 1,
Requested: 0
}
]
},
{
Category_Name: "License",
total: 3,
status: [
{
Available: 3,
Good_Condition: 0,
Requested: 0
}
]
},
{
Category_Name: "Laptop",
total: 2,
status: [
{
Available: 0,
Good_Condition: 2,
Requested: 0
}
]
},
{
Category_Name: "Vehicle",
total: 2,
status: [
{
Available: 0,
Good_Condition: 0,
Requested: 2
}
]
}
];
I can only solve up to getting total
{
Category_Name: "Furniture",
total: 2,
},
..
with
data.forEach(function(d) {
if (holder.hasOwnProperty(d.Category_Name)) {
holder[d.Category_Name] =
holder[d.Category_Name] + d.Count;
} else {
holder[d.Category_Name] = d.Count;
}
});
for (var prop in holder) {
obj2.push({ Category_Name: prop, total: holder[prop] });
}
and I am having issue getting status array, If someone could help me , I would very much appreciated, thanks.