按StudentId分组数组并实现考勤记录多维度统计的技术求助
嘿,我来帮你搞定这两个数组处理的需求!结合你提到的StackOverflow思路,我整理了对应的实现方案,一起来看看:
需求1:按StudentId分组并计算子数组总和
这个需求核心是把相同StudentId的元素归为一组,然后计算该组的总和。我们可以用JavaScript的Array.reduce()方法来高效实现,这也是社区里常用的方案:
// 示例输入数组(可以替换成你的实际数据) const studentData = [ { StudentId: 1, value: 10 }, { StudentId: 2, value: 20 }, { StudentId: 1, value: 15 }, { StudentId: 3, value: 5 }, { StudentId: 2, value: 25 } ]; // 按StudentId分组并计算总和 const groupedSumResult = studentData.reduce((accumulator, currentItem) => { // 如果当前StudentId还没在累加器中,初始化分组 if (!accumulator[currentItem.StudentId]) { accumulator[currentItem.StudentId] = { StudentId: currentItem.StudentId, totalSum: 0 }; } // 累加当前元素的值到对应分组 accumulator[currentItem.StudentId].totalSum += currentItem.value; return accumulator; }, {}); // 转换为数组格式(如果需要的话,否则直接用对象格式即可) const formattedResult = Object.values(groupedSumResult); console.log(formattedResult); // 输出:[{ StudentId: 1, totalSum: 25 }, { StudentId: 2, totalSum: 45 }, { StudentId: 3, totalSum: 5 }]
需求2:考勤记录数组转换为指定格式
这个需求需要分两步:先按「StudentId + subjectname」的组合统计present/absent的数量,再把同一StudentId的所有科目记录归到一组。同样用reduce来实现:
// 示例考勤记录数组(替换为你的实际数据) const attendanceData = [ { StudentId: 1, studentname: "张三", subjectname: "语文", present: true }, { StudentId: 1, studentname: "张三", subjectname: "语文", present: false }, { StudentId: 1, studentname: "张三", subjectname: "数学", present: true }, { StudentId: 2, studentname: "李四", subjectname: "语文", present: false }, { StudentId: 2, studentname: "李四", subjectname: "数学", present: true }, { StudentId: 2, studentname: "李四", subjectname: "数学", present: true } ]; // 第一步:统计每个学生每个科目的考勤数据 const subjectStats = attendanceData.reduce((acc, curr) => { // 用StudentId+subjectname作为唯一键,避免重复统计 const uniqueKey = `${curr.StudentId}-${curr.subjectname}`; if (!acc[uniqueKey]) { acc[uniqueKey] = { StudentId: curr.StudentId, studentname: curr.studentname, subjectname: curr.subjectname, present: 0, absent: 0, total: 0 }; } // 根据present状态计数 curr.present ? acc[uniqueKey].present++ : acc[uniqueKey].absent++; acc[uniqueKey].total++; return acc; }, {}); // 第二步:按StudentId分组,整理成最终格式 const finalAttendanceResult = Object.values(subjectStats).reduce((acc, curr) => { const studentKey = curr.StudentId; if (!acc[studentKey]) { acc[studentKey] = { StudentId: curr.StudentId, studentname: curr.studentname, subjects: [] }; } // 把科目统计信息加入对应学生的subjects数组 acc[studentKey].subjects.push({ subjectname: curr.subjectname, present: curr.present, absent: curr.absent, total: curr.total }); return acc; }, {}); // 转换为数组格式 const formattedAttendance = Object.values(finalAttendanceResult); console.log(formattedAttendance); /* 输出: [ { StudentId: 1, studentname: "张三", subjects: [ { subjectname: "语文", present: 1, absent: 1, total: 2 }, { subjectname: "数学", present: 1, absent: 0, total: 1 } ] }, { StudentId: 2, studentname: "李四", subjects: [ { subjectname: "语文", present: 0, absent: 1, total: 1 }, { subjectname: "数学", present: 2, absent: 0, total: 2 } ] } ] */
这两段代码都采用了reduce方法,它在处理数组分组、统计这类场景时非常灵活高效,也是StackOverflow社区里被广泛推荐的实现方式。你可以根据自己的实际数据结构调整字段名称,直接复用逻辑~
内容的提问来源于stack exchange,提问作者Darvesh
相关产品推荐
相关产品推荐

