我有一系列对象说temp。我想根据对象的属性进行分组。例如,应该对性别进行分组,并计算其数量。
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"
}
}
];
可以像这样传递需要分组的道具
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"]);
分组并累加计数后的最终结果应该是
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
},
]
]