简体   繁体   English

如何使用reduce计算总百分比,主题总数

[英]how to calculate total percentage, total number with subject by using reduce

I have a function subjectTotal which returns the total from a subject CQ and MCQ, I am trying to calculate the percentage inside the subjectTotal function, I have already a percentage function that returns the percentage and grade point.我有一个 function subjectTotal,它返回一个主题 CQ 和 MCQ 的总数,我正在尝试计算 subjectTotal function 中的百分比,我已经有一个百分比 ZC1C425268E68385DC14ZA504 返回百分比点。

SubjectTotal Function:主题总 Function:

 const value = { english_1st_CQ: 5, english_2nd_CQ: 10, math_1st_CQ: 15, math_1st_MCQ: 20, math_2nd_CQ: 25, math_2nd_MCQ: 30, }; const subjectTotal = Object.entries(value).reduce((result, [key, value]) => { const [subjectName, subjectPosition, suffix] = key.split('_') const fullSubjectName = `${subjectName}_${subjectPosition}` if(;result[fullSubjectName]) { result[fullSubjectName] = 0, } result[fullSubjectName] += value return result }. {}) console log(subjectTotal)

Calculate percentage function, also I am calculating the Grade point inside the function计算百分比 function,我也在计算 function 内的成绩点

 const subjectTotal = { english_1st: 5, english_2nd: 10, math_1st: 35, math_2nd: 55 } const calculatePercentage = (subTotal) => { if (subTotal === 0) return { percentage: 0, GP: 0 }; // console.log(subTotal); const percentage = (subTotal / 100) * 100 - 4; let GP; if (percentage >= 80) GP = 5; if (percentage >= 70 && percentage <= 79) GP = 4; if (percentage >= 60 && percentage <= 69) GP = 3.5; if (percentage >= 50 && percentage <= 59) GP = 3; if (percentage >= 40 && percentage <= 49) GP = 2; if (percentage >= 33 && percentage <= 39) GP = 1; if (percentage < 33) GP = 0; return { percentage, GP }; }; const math_percentage = calculatePercentage(subjectTotal.math_1st + subjectTotal.math_2nd); const english_percentage= calculatePercentage(subjectTotal.english_2nd + subjectTotal.english_1st); console.log(math_percentage, english_percentage)

I don't want to call the function again and again, like this way math_percentage , english_percentage .我不想一次又一次地调用 function,就像这样math_percentageenglish_percentage so I am trying to call the calculatePercentage inside the subjectTotal and the subject total will return the subject total also the percentage所以我试图在subjectTotal中调用calculatePercentage,并且主题总数将返回主题总数以及百分比

I don't have an example I just want to calculate the percentage inside the subTotal.我没有示例,我只想计算 subTotal 中的百分比。

Here is what you need这是你需要的

const value = {
  english_1st_CQ: 5,
  english_2nd_CQ: 10,
  math_1st_CQ: 15,
  math_1st_MCQ: 20,
  math_2nd_CQ: 25,
  math_2nd_MCQ: 30,
};

const calculatePercentage = (subTotal) => {
  if (subTotal === 0) return { percentage: 0, GP: 0 };
  const percentage = (subTotal / 100) * 100 - 4;
  let GP;
  if (percentage >= 80) GP = 5;
  if (percentage >= 70 && percentage <= 79) GP = 4;
  if (percentage >= 60 && percentage <= 69) GP = 3.5;
  if (percentage >= 50 && percentage <= 59) GP = 3;
  if (percentage >= 40 && percentage <= 49) GP = 2;
  if (percentage >= 33 && percentage <= 39) GP = 1;
  if (percentage < 33) GP = 0;
  return { percentage, GP };
};

const subjectTotal = Object.entries(value).reduce((result, [key, value], currentIndex, array) => {
   const [subjectName, subjectPosition, suffix] = key.split('_')
   const fullSubjectName = `${subjectName}_${subjectPosition}`
   if(!result[fullSubjectName]) {
       result[fullSubjectName] = 0;
   }
   result[fullSubjectName] += value;

   
   if (currentIndex + 1 === array.length){
       result.math_percentage = calculatePercentage(result.math_1st + result.math_2nd);
       result.english_percentage= calculatePercentage(result.english_2nd + result.english_1st);
   }
   return result
}, {})

console.log(subjectTotal);

In the above code I use the calculatePercentage only in the last iteration of reduce for last element where all the elements of the array that you have provided are already reduced to result that you want.在上面的代码中,我仅在最后一个元素的 reduce 的最后一次迭代中使用了calculatePercentage ,其中您提供的数组的所有元素都已减少为您想要的结果。 Then we attach to result the percentages according to the already final calculated values it contains.然后我们根据它包含的已经最终计算的值将百分比附加到结果中。

Also as you can see in signature reduce((result, [key, value], currentIndex, array) reduce allows as parameter the currentIndex which will show which iteration the reduce method is applied to, and also the array that that was provided as input to be reduced.此外,正如您在签名中看到的那样reduce((result, [key, value], currentIndex, array) reduce 允许将 currentIndex 作为参数,这将显示 reduce 方法应用于哪个迭代,以及作为输入提供的数组被减少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM