将JSON数据转换为计算总出勤率的内容。

huangapple go评论93阅读模式
英文:

JSON data to calculate the total attendance in percentage?

问题

You can calculate the total attendance for a student with the given data by counting the key-value pairs with a value of 1 in the JSON object. Here's a JavaScript code snippet to do that:

  1. const studentData = {
  2. "CGMM": {
  3. "17:4:2023": 1,
  4. "19:4:2023": 0
  5. },
  6. "CNS": {
  7. "17:4:2023": 1
  8. },
  9. "ML": {
  10. "17:4:2023": 1,
  11. "16:4:2023": 1
  12. }
  13. };
  14. let totalPresent = 0;
  15. let totalAttendance = 0;
  16. for (const subject in studentData) {
  17. for (const date in studentData[subject]) {
  18. if (studentData[subject][date] === 1) {
  19. totalPresent++;
  20. }
  21. totalAttendance++;
  22. }
  23. }
  24. const attendancePercentage = (totalPresent / totalAttendance) * 100;
  25. console.log("Total Attendance Percentage: " + attendancePercentage + "%");

This code will count the key-value pairs with a value of 1 and calculate the total attendance percentage for the student.

英文:

I have this attendance data for a particular student. If he was present then marked 1 else marked 0 in the respected subject. How I can calculate the total attendance out of it. Please help me out...

  1. {
  2. "CGMM": {
  3. "17:4:2023": 1,
  4. "19:4:2023": 0
  5. },
  6. "CNS": {
  7. "17:4:2023": 1
  8. },
  9. "ML": {
  10. "17:4:2023": 1,
  11. "16:4:2023": 1
  12. }
  13. }

I need that I can count all those key-value pair which has a value equals 1 and then divide it by total no. of key-value pairs exist which I am finding by:

  1. const attendenceInML = Object.keys(student.ML).length;
  2. const attendenceInSEA = Object.keys(student.SEA).length;
  3. const attendenceInCGMM = Object.keys(student.CGMM).length;
  4. const attendenceInCNS = Object.keys(student.CNS).length;
  5. const total = attendenceInML+ attendenceInSEA+ attendenceInCGMM+ attendenceInCNS;

and then multipy the resultant with 100 to get the total attendance in percentage, but I don't know how to fetch only those key-value pairs count which has value as 1. Please help me.

答案1

得分: 0

代码部分不需要翻译,以下是已翻译的内容:

"格式相当糟糕,但您仍然可以使用 for in 语法迭代对象:"

"const data = JSON.parse(input);"

"let totalClasses = 0;"
"let attendedClasses = 0;"

"for (const subject in data) {"
" if (data.hasOwnProperty(subject)) { //以防对象为空而引发运行时错误"
" const dates = data[subject];"
" for (const date in dates) {"
" if (dates.hasOwnProperty(date)) {"
" totalClasses++;"
" attendedClasses += dates[date];"
" }"
" }"
" }"
"}"

"const attendancePercentage = (attendedClasses / totalClasses) * 100;"

英文:

The format is pretty terrible, but you can still iterate over objects with for in syntax:

  1. const data = JSON.parse(input);
  2. let totalClasses = 0;
  3. let attendedClasses = 0;
  4. for (const subject in data) {
  5. if (data.hasOwnProperty(subject)) { //So that we don't get a runtime error if the object is empty
  6. const dates = data[subject];
  7. for (const date in dates) {
  8. if (dates.hasOwnProperty(date)) {
  9. totalClasses++;
  10. attendedClasses += dates[date];
  11. }
  12. }
  13. }
  14. }
  15. const attendancePercentage = (attendedClasses / totalClasses) * 100;

答案2

得分: 0

你可以使用这段代码来减少数据并计算总和/平均值。类似这样的:

  1. const data = {
  2. "CGMM": {
  3. "17:4:2023": 1,
  4. "19:4:2023": 0
  5. },
  6. "CNS": {
  7. "17:4:2023": 1
  8. },
  9. "ML": {
  10. "17:4:2023": 1,
  11. "16:4:2023": 1
  12. }
  13. };
  14. const totalAttendance = Object.values(data)
  15. .reduce((acc, v) => acc.concat(Object.values(v)), []);
  16. const sum = totalAttendance.reduce((acc, v) => acc + v, 0);
  17. const attendance = sum / totalAttendance.length * 100;
  18. console.log(`value 1的总和: ${sum}\n出勤百分比: ${attendance}%`);

请注意,我只提供了代码的翻译部分。

英文:

You can reduce the data and calculate sum/average with it. Something like:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const data = {
  2. &quot;CGMM&quot;: {
  3. &quot;17:4:2023&quot;: 1,
  4. &quot;19:4:2023&quot;: 0
  5. },
  6. &quot;CNS&quot;: {
  7. &quot;17:4:2023&quot;: 1
  8. },
  9. &quot;ML&quot;: {
  10. &quot;17:4:2023&quot;: 1,
  11. &quot;16:4:2023&quot;: 1
  12. }
  13. };
  14. const totalAttendance = Object.values(data)
  15. .reduce( (acc, v) =&gt; acc.concat(Object.values(v)), [] );
  16. const sum = totalAttendance.reduce( (acc, v) =&gt; acc + v, 0 );
  17. const attendance = sum / totalAttendance.length * 100;
  18. console.log( `sum of value 1: ${sum}\nattendance percentage: ${
  19. attendance}%` );

<!-- end snippet -->

答案3

得分: 0

你可以使用Object.entriesObject.values.reduce的组合来完成它。

  1. let student = {
  2. "CGMM": {
  3. "17:4:2023": 1,
  4. "19:4:2023": 0
  5. },
  6. "CNS": {
  7. "17:4:2023": 1
  8. },
  9. "ML": {
  10. "17:4:2023": 1,
  11. "16:4:2023": 1
  12. }
  13. }
  14. let result = Object.entries(student)
  15. .reduce((prev, [key, objValue]) => prev + Object.values(objValue)
  16. .reduce((prev, curr) => prev + curr, 0), 0)
  17. console.log(result)

注意:我只提供代码的翻译,不回答翻译问题。

英文:

You can do it with an combination of Object.entries, Object.values and .reduce

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let student = {
  2. &quot;CGMM&quot;: {
  3. &quot;17:4:2023&quot;: 1,
  4. &quot;19:4:2023&quot;: 0
  5. },
  6. &quot;CNS&quot;: {
  7. &quot;17:4:2023&quot;: 1
  8. },
  9. &quot;ML&quot;: {
  10. &quot;17:4:2023&quot;: 1,
  11. &quot;16:4:2023&quot;: 1
  12. }
  13. }
  14. let result = Object.entries(student)
  15. .reduce((prev, [key, objValue]) =&gt; prev + Object.values(objValue)
  16. .reduce((prev,curr) =&gt; prev + curr,0),0)
  17. console.log(result)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月19日 16:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定