I have an array of objects say temp. I want to group based on the properties of object. For example, the gender should be grouped and its count also be calculated.
const temp = [
{
properties: {
"id":1234,
"gender": 'male',
"status": "Active"
}
},
{
properties: {
"id":1456,
"gender": 'male',
"status": "Not Active"
}
},
{
properties: {
"id":1377,
"gender": 'female',
"status": "Active"
}
},
{
properties: {
"id":8799,
"gender": 'female',
"status": "Active"
}
}
];
The needed props to be grouped can be passed like
groupFunc = (data) => {
const metrics = data
for (i = 0; i < temp.length; i++) {
data.map( el =>
temp[i].properties.el ? grouped.push([{"key": el, "value": temp[i].properties.el,"count":1}])
:
null
)
console.log(temp[i].properties)
}
};
groupFunc(["gender","status"]);
The end result after grouping and accumulating its count should be
grouped = [
[
{
"key": "gender",
"value": "male",
"count": 2
},
{
"key": "gender",
"value": "female",
"count": 2
}
],
[
{
"key": "status",
"value": "Active",
"count": 3
},
{
"key": "status",
"value": "Not Active",
"count": 1
},
]
]