[{
"xh": "2022050601",
"score": 2.17,
"clzTitle": "springboot班级1",
"targetid": 1,
"nickName": "张同学",
"tcourseId": 10,
"targetTitle": "目标1",
"clzId": 10,
"id": 3889,
"type": "作业",
"userId": 113,
"pjtype": 1
},...]
{"2022050601": {
"xh": "2022050601",
"nickName": "张同学",
"clzTitle": "springboot班级1",
"score": 37.27,
"detail": [
{
"xh": "2022050601",
"score": 2.17,
"clzTitle": "springboot班级1",
"targetid": 1,
"nickName": "张同学",
"tcourseId": 10,
"targetTitle": "目标1",
"clzId": 10,
"id": 3889,
"type": "作业",
"userId": 113,
"pjtype": 1
},
...
]
}
// js分组求和
let stusum = {};
data.map(item=>{
if(!stusum[item.xh]){
stusum[item.xh] ={xh:item.xh, nickName:item.nickName, clzTitle: item.clzTitle, score: 0, detail: []};
}
stusum[item.xh].score += item.score;
stusum[item.xh].detail.push(item);
});
// 将List按某个字段分组,将分组的集合添加到分组后对象的children属性
groupByDeptId(rows = []) {
// 使用了聚合分组
let ids = rows.reduce((pre, cur) => {
(pre[cur.deptId] = pre[cur.deptId] || []).push(cur);
return pre;
}, {});
ids = ids || {};
// 封装自己需要的对象
let list = [];
for (let i in ids) {
list.push({ id: i, name: ids[i][0].deptName, children: ids[i] });
}
this.dataList = list;
// console.log(list);
},
// java分组求和
Map<Object, BigDecimal> collect =
scoreList.stream().collect(Collectors.groupingBy(item -> item.get("xh").toString(),
Collectors.reducing(BigDecimal.ZERO, t -> (BigDecimal)t.get("score"), BigDecimal::add)));
{2022050601=37.2700, 2022050603=29.1000, 2022050606=23.3000, 2022050608=19.5000}
List<Map<String, Object>> scoreList = bizPjMutilscoreMapper.selectBizPjMutilscoreListByCourseid(courseid);
List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
Map<String, List<Map<String, Object>>> gs =
scoreList.stream().collect(Collectors.groupingBy(e -> e.get("xh").toString()));
gs.forEach((k, v) -> {
Map<String, Object> tmp = new HashMap<String, Object>();
// 按属性排序
v.sort(Comparator.comparing(t -> Integer.valueOf(t.get("pjtype").toString())));
Map<String, BigDecimal> s = v.stream().collect(Collectors.groupingBy(e -> e.get("xh").toString(),
Collectors.reducing(BigDecimal.ZERO, t -> new BigDecimal(t.get("score").toString()), BigDecimal::add)));
tmp.put("xh", k);
tmp.put("nickName", v.get(0).get("nickName").toString());
tmp.put("clzTitle", v.get(0).get("clzTitle").toString());
tmp.put("score", s.get(k));
tmp.put("detail", v);
res.add(tmp);
});
未经允许不得转载:
红吉他 »
ES6 »
对象数组分组求和以及将数组按某个字段分组