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

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

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:

const studentData = {
  "CGMM": {
    "17:4:2023": 1,
    "19:4:2023": 0
  },
  "CNS": {
    "17:4:2023": 1
  },
  "ML": {
    "17:4:2023": 1,
    "16:4:2023": 1
  }
};

let totalPresent = 0;
let totalAttendance = 0;

for (const subject in studentData) {
  for (const date in studentData[subject]) {
    if (studentData[subject][date] === 1) {
      totalPresent++;
    }
    totalAttendance++;
  }
}

const attendancePercentage = (totalPresent / totalAttendance) * 100;

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...

    {
      "CGMM": {
        "17:4:2023": 1,
        "19:4:2023": 0
      },
      "CNS": {
        "17:4:2023": 1
      },
      "ML": {
        "17:4:2023": 1,
        "16:4:2023": 1
      }
    }

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:

        const attendenceInML = Object.keys(student.ML).length;
        const attendenceInSEA = Object.keys(student.SEA).length;
        const attendenceInCGMM = Object.keys(student.CGMM).length;
        const attendenceInCNS = Object.keys(student.CNS).length;
        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:

const data = JSON.parse(input);

let totalClasses = 0;
let attendedClasses = 0;

for (const subject in data) {
  if (data.hasOwnProperty(subject)) { //So that we don't get a runtime error if the object is empty
    const dates = data[subject];
    for (const date in dates) {
      if (dates.hasOwnProperty(date)) {
        totalClasses++;
        attendedClasses += dates[date];
      }
    }
  }
}

const attendancePercentage = (attendedClasses / totalClasses) * 100;

答案2

得分: 0

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

const data = {
  "CGMM": {
    "17:4:2023": 1,
    "19:4:2023": 0
  },
  "CNS": {
    "17:4:2023": 1
  },
  "ML": {
    "17:4:2023": 1,
    "16:4:2023": 1
  }
};
const totalAttendance = Object.values(data)
  .reduce((acc, v) => acc.concat(Object.values(v)), []);
const sum = totalAttendance.reduce((acc, v) => acc + v, 0);
const attendance = sum / totalAttendance.length * 100;
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 -->

const data = {
  &quot;CGMM&quot;: {
    &quot;17:4:2023&quot;: 1,
    &quot;19:4:2023&quot;: 0
  },
  &quot;CNS&quot;: {
    &quot;17:4:2023&quot;: 1
  },
  &quot;ML&quot;: {
    &quot;17:4:2023&quot;: 1,
    &quot;16:4:2023&quot;: 1
  }
};
const totalAttendance = Object.values(data)
  .reduce( (acc, v) =&gt; acc.concat(Object.values(v)), [] );
const sum = totalAttendance.reduce( (acc, v) =&gt; acc + v, 0 );
const attendance = sum / totalAttendance.length * 100;
console.log( `sum of value 1: ${sum}\nattendance percentage: ${
  attendance}%` );

<!-- end snippet -->

答案3

得分: 0

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

let student = {
    "CGMM": {
        "17:4:2023": 1,
        "19:4:2023": 0
    },
    "CNS": {
        "17:4:2023": 1
    },
    "ML": {
        "17:4:2023": 1,
        "16:4:2023": 1
    }
}

let result = Object.entries(student)
    .reduce((prev, [key, objValue]) => prev + Object.values(objValue)
        .reduce((prev, curr) => prev + curr, 0), 0)

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 -->

let student = {
      &quot;CGMM&quot;: {
        &quot;17:4:2023&quot;: 1,
        &quot;19:4:2023&quot;: 0
      },
      &quot;CNS&quot;: {
        &quot;17:4:2023&quot;: 1
      },
      &quot;ML&quot;: {
        &quot;17:4:2023&quot;: 1,
        &quot;16:4:2023&quot;: 1
      }
    }
    
    
let result = Object.entries(student)
               .reduce((prev, [key, objValue]) =&gt; prev + Object.values(objValue)
               .reduce((prev,curr) =&gt; prev + curr,0),0)

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:

确定