根据属性和javascript中的计数按对象数组分组

根据属性和javascript中的计数按对象数组分组,javascript,Javascript,我有一个对象数组,比如temp。我想根据对象的属性进行分组。例如,应将性别分组并计算其计数 const temp = [ { properties: { "id":1234, "gender": 'male', "status": "Active" } }, { properties: { "id":1456,

我有一个对象数组,比如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"]);
const temp=[{properties:{id:1234,“性别”:“男性”,“状态”:“活动”},{properties:{id:1456,“性别”:“男性”,“状态”:“非活动”},{properties:{id:1377,“性别”:“女性”,“状态”:“活动”},{properties:{id:8799,“性别”:“女性”,“状态”:“活动”}];
const groupByKeys=(数据、键)=>{
让finalResult=keys.map(key=>{
返回Object.values(data.reduce)((result,obj)=>{
设objKey=obj[“属性”][键]
result[objKey]=result[objKey]|{key:key,count:0,value:objKey};
结果[objKey]。计数+=1;
返回结果
},{}))
})
返回最终结果
}
日志(GroupByKey(临时、[“性别”、“状态”))
const temp=[{properties:{id:1234,“性别”:“男性”,“状态”:“活动”},{properties:{id:1456,“性别”:“男性”,“状态”:“非活动”},{properties:{id:1377,“性别”:“女性”,“状态”:“活动”},{properties:{id:8799,“性别”:“女性”,“状态”:“活动”}];
const groupByKeys=(数据、键)=>{
让finalResult=keys.map(key=>{
返回Object.values(data.reduce)((result,obj)=>{
设objKey=obj[“属性”][键]
result[objKey]=result[objKey]|{key:key,count:0,value:objKey};
结果[objKey]。计数+=1;
返回结果
},{}))
})
返回最终结果
}
日志(GroupByKey(临时、[“性别”、“状态”))
const temp=[
{
特性:{
“id”:1234,
“性别”:“男性”,
“状态”:“活动”
}
},
{
特性:{
“id”:1456,
“性别”:“男性”,
“状态”:“未激活”
}
},
{
特性:{
“id”:1377,
“性别”:“女性”,
“状态”:“活动”
}
},
{
特性:{
“id”:8799,
“性别”:“女性”,
“状态”:“活动”
}
}
];
常数计数=(arr,key)=>arr.reduce((acc,curr)=>{
设k=curr[key]
如果(!acc.map(a=>a.value).包括(k)){
acc=[…acc{
钥匙
值:k,
计数:1
}]
}
acc.map(a=>a.value==k?{…a,count:a.count+1}:a)
返回acc;
} , [])
数据=温度映射(t=>t.properties)
groupFunc=(keys)=>keys.map(k=>count(数据,k))
const res=groupFunc([“性别”,“状态]);
console.log(res)
const temp=[
{
特性:{
“id”:1234,
“性别”:“男性”,
“状态”:“活动”
}
},
{
特性:{
“id”:1456,
“性别”:“男性”,
“状态”:“未激活”
}
},
{
特性:{
“id”:1377,
“性别”:“女性”,
“状态”:“活动”
}
},
{
特性:{
“id”:8799,
“性别”:“女性”,
“状态”:“活动”
}
}
];
常数计数=(arr,key)=>arr.reduce((acc,curr)=>{
设k=curr[key]
如果(!acc.map(a=>a.value).包括(k)){
acc=[…acc{
钥匙
值:k,
计数:1
}]
}
acc.map(a=>a.value==k?{…a,count:a.count+1}:a)
返回acc;
} , [])
数据=温度映射(t=>t.properties)
groupFunc=(keys)=>keys.map(k=>count(数据,k))
const res=groupFunc([“性别”,“状态]);

console.log(res)
您可以使用
reduce
实现所需的功能,如下所示

在代码中添加了解释

const temp=[{properties:{id:1234,“性别”:“男性”,“状态”:“活动”},{properties:{id:1456,“性别”:“男性”,“状态”:“非活动”},{properties:{id:1377,“性别”:“女性”,“状态”:“活动”},{properties:{id:8799,“性别”:“女性”,“状态”:“活动”}];
const groupFunc=(数据、道具)=>{
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
//reduce有两个参数回调函数和initialValue:可选。
//这里有回调函数(a,I)=>{…}
//添加初始值=[]
让resultGroups=data.reduce((a,i)=>{
//循环遍历每个属性
props.forEach(p=>{
//获取与结果对象a和当前对象数据匹配的值的索引。
让index=a.findIndex(x=>x.key==p&&x.value==i.properties[p]);
//如果index=-1,则不存在这样的值。所以添加新值。否则递增计数
如果(索引==-1){
a、 push({key:p,value:i.properties[p],count:1});
}否则{
a[index].count++;
}
});
//返回结果对象(reduce函数中的aka累加器)
返回a;
}, []);
让结果=[];
//循环遍历每个属性并为每个属性创建组。
props.forEach(p=>{
//检查此属性是否有可用的对象。
//如果是,则过滤对象并推入结果数组。
if(resultGroups.some(x=>x.key==p)){
push(resultGroups.filter(x=>x.key==p));
}
});
//返回结果。
返回结果;
};

日志(groupFunc(临时,[“性别”,“状态]))
您可以使用
reduce
实现所需的功能,如下所示

在代码中添加了解释

const temp=[{properties:{id:1234,“性别”:“男性”,“状态”:“活动”},{properties:{id:1456,“性别”:“男性”,“状态”:“非活动”},{properties:{id:1377,“性别”:“女性”,“状态”:“活动”},{properties:{id:8799,“性别”:“女性”,“状态”:“活动”}];
const groupFunc=(数据、道具)=>{
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
//reduce有两个参数回调函数和initialValue:可选。
//这里有回调函数(a,I)=>{…}
//添加初始值=[]
让resultGroups=data.reduce((a,i)=>{
//循环遍历每个属性
props.forEach(p=>{
//获取与结果对象匹配的值的索引
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
    },
  ]

]