简体   繁体   English

计算一个值在嵌套对象的多级数组中出现的次数

[英]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.所以我有一个数组对象,其中包含startDateendDate和一个学生对象数组,其中包含学生的fullNamedocIdattendanceStatus课程的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.

相关问题 计算一个值在对象数组中出现的次数 - Count how many times a value occurs in a array of objects Javascript JSON对象的多级数组 - 如何在第二级或更高级别访问键值对 - Javascript Multi-level array of JSON objects - how to access key-value pair in second level or higher 如何提取嵌套的多层JSON对象/数组的部分并使用JavaScript对其进行展平 - How to extract parts of nested multi-level JSON objects / array and flatten it with JavaScript 如何访问多级对象数组? - How can I access an array of multi-level objects? 按属性对对象数组进行排序(多级) - Sorting array of objects by properties (multi-level) 在JavaScript中展平多级/嵌套数组 - Flatten multi-level/nested array in javascript 计算特定检查值在AngularJS中出现的次数 - Counting how many times a specific checked value occurs in AngularJS 从对象的多层嵌套数组reactjs创建嵌套的JSX列表项 - Create nested JSX list items from multi-level nested array of objects reactjs 从多级嵌套对象数组 reactjs 创建嵌套的 JSX 元素 - Create nested JSX elements from multi-level nested array of objects reactjs 计算值在数组中出现的次数-Javascript-angular-lodash - Count how many times a value occurs in an array - javascript - angular - lodash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM