In my Angular 9 application, I am looking to separate an array based on the lable
field. Within each separated array, I would like to determine the count based on the subLable
field.
This is the array I am working with:
[
{"id":1,"socialMediaId":0,"accountId":1,"lable":"Application","subLable":"Nice"},
{"id":2,"socialMediaId":0,"accountId":1,"lable":"Channels","subLable":"Quality"},
{"id":3,"socialMediaId":0,"accountId":1,"lable":"Application","subLable":"poor"},
{"id":4,"socialMediaId":0,"accountId":1,"lable":"Application","subLable":"Nice"}
]
The lable
: Application appears 3 times and lable
: Channels appears once in the array. I aim to split the array based on the lable
as follows:
Application[
{"id":1,"socialMediaId":0,"accountId":1,"lable":"Application","subLable":"Nice"},
{"id":3,"socialMediaId":0,"accountId":1,"lable":"Application","subLable":"poor"},
{"id":4,"socialMediaId":0,"accountId":1,"lable":"Application","subLable":"Nice"}
]
Channels[
{"id":2,"socialMediaId":0,"accountId":1,"lable":"Channels","subLable":"Quality"},
]
To calculate the counts for each unique value in subLable
, the final result should be summarized like this:
Application subLable Nice count 2, poor count 1
I have attempted the following code snippet:
this.smdashboardservice.fetchSmDashboardData().subscribe(response=>{
const lable = response.data.reduce((acc, v) => {
acc[v.lable] = acc[v.lable] || [];
acc[v.lable].push(v);
return acc;
}, {});
console.log(lable)
})
Current outcome: