[英]Counting how many times a value occurs in multi-level array of nested objects
So I've got an array objects that contains the startDate
, endDate
and an array of student objects which contains the student's fullName
, docId
and attendanceStatus
for a course for a class.所以我有一个数组对象,其中包含startDate
、 endDate
和一个学生对象数组,其中包含学生的fullName
、 docId
和attendanceStatus
课程的attendanceStatus
。 I'm not allowed to change the way the data is structured.我不允许改变数据的结构方式。
[
{
startDate: Mon Jul 26 2021 08:19:00 GMT+0800 (Philippine Standard Time),
endDate: Mon Jul 26 2021 09:19:00 GMT+0800 (Philippine Standard Time),
id: "EE201_433",
title: "EE201",
students: [
{docId: "LKfpyWyWuXlbBLUBoRp2", attendanceStatus: "Present", fullName: "John Smith"},
{docId: "nwHWP3iNjGm2tEODsaLo", attendanceStatus: "Absent", fullName: "Claire Smith"},
{docId: "x9TLXbsVfgpZfQU7aj8Z", attendanceStatus: "Excused", fullName: "Jane Smith"}
]
},
{
startDate: Mon Jul 19 2021 08:19:00 GMT+0800 (Philippine Standard Time),
endDate: Mon Jul 19 2021 09:19:00 GMT+0800 (Philippine Standard Time),
id: "EE201_432",
title: "EE201",
students: [
{docId: "LKfpyWyWuXlbBLUBoRp2", attendanceStatus: "Absent", fullName: "John Smith"},
{docId: "nwHWP3iNjGm2tEODsaLo", attendanceStatus: "Present", fullName: "Claire Smith"},
{docId: "x9TLXbsVfgpZfQU7aj8Z", attendanceStatus: "Excused", fullName: "Jane Smith"}
]
}
]
How can I create a function that takes the student's docId
as an argument then returns something like this.我如何创建一个函数,将学生的docId
作为参数,然后返回这样的内容。 How can I count how many times a student is absent, present or excused?我如何计算学生缺席、出席或请假的次数?
{
LKfpyWyWuXlbBLUBoRp2: {present: 1, absent: 1, excused: 0},
}
I don't know if this is the best way to do this but this works我不知道这是否是最好的方法,但这有效
const courses = [ { startDate: "Mon Jul 26 2021 08:19:00 GMT+0800 (Philippine Standard Time)", endDate: "Mon Jul 26 2021 09:19:00 GMT+0800 (Philippine Standard Time)", id: "EE201_433", title: "EE201", students: [ {docId: "LKfpyWyWuXlbBLUBoRp2", attendanceStatus: "Present", fullName: "John Smith"}, {docId: "nwHWP3iNjGm2tEODsaLo", attendanceStatus: "Absent", fullName: "Claire Smith"}, {docId: "x9TLXbsVfgpZfQU7aj8Z", attendanceStatus: "Excused", fullName: "Jane Smith"} ] }, { startDate: "Mon Jul 19 2021 08:19:00 GMT+0800 (Philippine Standard Time)", endDate: "Mon Jul 19 2021 09:19:00 GMT+0800 (Philippine Standard Time)", id: "EE201_432", title: "EE201", students: [ {docId: "LKfpyWyWuXlbBLUBoRp2", attendanceStatus: "Absent", fullName: "John Smith"}, {docId: "nwHWP3iNjGm2tEODsaLo", attendanceStatus: "Present", fullName: "Claire Smith"}, {docId: "x9TLXbsVfgpZfQU7aj8Z", attendanceStatus: "Excused", fullName: "Jane Smith"} ] }, ] const id = "LKfpyWyWuXlbBLUBoRp2"; const getStudentAttendance = (id) => { let student = { absent: 0, present: 0, excused: 0 } courses.forEach(course => { const { attendanceStatus } = course.students.find(({ docId }) => docId === id) if (attendanceStatus) { student[attendanceStatus.toLowerCase()] += 1 } }) return { [id]: student } } console.log(getStudentAttendance(id))
document.getElementById("btn").addEventListener('click', () => { const data = [ { startDate: "Mon Jul 26 2021 08:19:00 GMT+0800 (Philippine Standard Time)", endDate: "Mon Jul 26 2021 09:19:00 GMT+0800 (Philippine Standard Time)", id: "EE201_433", title: "EE201", students: [ {docId: "LKfpyWyWuXlbBLUBoRp2", attendanceStatus: "Present", fullName: "John Smith"}, {docId: "nwHWP3iNjGm2tEODsaLo", attendanceStatus: "Absent", fullName: "Claire Smith"}, {docId: "x9TLXbsVfgpZfQU7aj8Z", attendanceStatus: "Excused", fullName: "Jane Smith"} ] }, { startDate: "Mon Jul 19 2021 08:19:00 GMT+0800 (Philippine Standard Time)", endDate: "Mon Jul 19 2021 09:19:00 GMT+0800 (Philippine Standard Time)", id: "EE201_432", title: "EE201", students: [ {docId: "LKfpyWyWuXlbBLUBoRp2", attendanceStatus: "Absent", fullName: "John Smith"}, {docId: "nwHWP3iNjGm2tEODsaLo", attendanceStatus: "Present", fullName: "Claire Smith"}, {docId: "x9TLXbsVfgpZfQU7aj8Z", attendanceStatus: "Excused", fullName: "Jane Smith"} ] }, ] var present = 0; var absent = 0; var excused = 0; var id = document.getElementById('id').value; for (var A = 0; A < data.length; A++){ for(var B = 0; B < data[A].students.length; B++){ var attendance = data[A].students[B].attendanceStatus if(data[A].students[B].docId == id){ if(attendance == 'Present'){present++} else if(attendance == 'Absent'){absent++} else{excused++} } } } var node = document.createElement("LI"); var textnode = document.createTextNode(id + ': ' + ' present: ' + present + ', absent: ' + absent + ', excused: ' + excused); node.appendChild(textnode); document.getElementById("list").appendChild(node); })
<label>Enter ID</label> <input id="id"> <button id="btn">Get attendance</button> <ul id="list"></ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.