简体   繁体   English

在 JavaScript 中对 JSON 数据执行聚合。 我必须得到每所学校的记录总数和学校记录的总分

[英]Perform an aggregation on JSON data in JavaScript. I have to get the total number of Records for each school and total Marks of School Records

I have input data in the Below format:我有以下格式的输入数据:

{
 "data": {
  "listStudents": {
   "items": [
    {
     "Marks": 5,
     "Grade": "Grade6",
     "SchoolName": "New St. Maria School",
     "SkillsetName": "Reading",
     "StudentName": "Abdur Rahman"
    },
    {
     "Marks": 5,
     "Grade": "Grade6",
     "SchoolName": "New St. Maria School",
     "SkillsetName": "Numeracy",
     "StudentName": "Munazza Maryam"
    },
    {
     "Marks": 3,
     "Grade": "Grade3",
     "SchoolName": "New St. Maria School",
     "SkillsetName": "Numeracy",
     "StudentName": "Y. Divya sri"
    },
    {
     "Marks": 4,
     "Grade": "Grade4",
     "SchoolName": "New St. Maria High School",
     "SkillsetName": "Reading",
     "StudentName": "Syed Soheb Ahmed"
    },
    {
     "Marks": 2,
     "Grade": "Grade3",
     "SchoolName": "New St. Maria High School",
     "SkillsetName": "Numeracy",
     "StudentName": "Mohammed Hanzala Sharieff"
    },
    {
     "Marks": 3,
     "Grade": "Grade5",
     "SchoolName": "New St. Maria High School",
     "SkillsetName": "Numeracy",
     "StudentName": "Syed Sulaiman"
    },
    {
     "Marks": 5,
     "Grade": "Grade6",
     "SchoolName": "New St. Maria High",
     "SkillsetName": "Reading",
     "StudentName": "Mohammed Shariq"
    },
    {
     "Marks": 2,
     "Grade": "Grade6",
     "SchoolName": "Sri Siddhartha High",
     "SkillsetName": "Reading",
     "StudentName": "SUMASRI"
    },
    {
     "Marks": 3,
     "Grade": "Grade4",
     "SchoolName": "New St. Maria High",
     "SkillsetName": "Reading",
     "StudentName": "Huda Anjum"
    },
    {
     "Marks": 5,
     "Grade": "Grade4",
     "SchoolName": "New St. Maria High",
     "SkillsetName": "Reading",
     "StudentName": "Nuzhath Ruqhaiya"
    }
   ]
  }
 }
}

I have to perform an Aggregation in Javascript to get the output as below:我必须在 Javascript 中执行聚合才能获得如下输出:

{list: 
[{
SchoolName: New St. Maria High,
TotalMarks:13,
TotalCount:3
},
{
SchoolName: Sri Siddhartha High,
TotalMarks:2,
TotalCount:1
},
{
SchoolName: New St. Maria High School,
TotalMarks:9,
TotalCount:3
},
{
SchoolName: New St. Maria High,
TotalMarks:10,
TotalCount:3
}]
}

Below is the logic that I Have written:下面是我写的逻辑:

    const uniqueTags = [];
    var SchoolName = null;
    var Sum = 0;
    Object.values(result.data.listStudents.items).forEach((x) => {
      if (SchoolName === x.SchoolName) {
        Sum = Sum + x.Marks;
        SchoolName = x.SchoolName;
      } else if (SchoolName === null) {
        Sum = x.Marks;
        SchoolName = x.SchoolName;
      } else if (SchoolName != x.SchoolName) {
        uniqueTags.push(Sum, SchoolName);
        SchoolName = x.SchoolName;
        Sum = x.Marks;
      }
    });

    console.log(uniqueTags);

But I am not getting any output of the code.但我没有得到任何代码输出。 Can I get some help to get it working as per the requirement?我可以获得一些帮助以使其按照要求工作吗? I tried the map function too but I am not able to get it working by any means.我也尝试了地图功能,但无论如何我都无法让它工作。 Any kind of help is appreciated here.任何形式的帮助在这里表示赞赏。

I would recommend to use the Array.prototype.reduce-function like this:我建议像这样使用 Array.prototype.reduce-function:

let aggregatedData = jsonData.data.listStudents.items.reduce((prev,curr) => {
    const agg = prev.list.find((s) => s.SchoolName == curr.SchoolName);
    if(!agg){
        prev.list.push({SchoolName: curr.SchoolName, TotalMarks: curr.Marks, TotalCount: 1});
    } else {
        agg.TotalMarks += curr.Marks;
        agg.TotalCount++;
    }
    return prev;

}, {list: []});

I hope this solves yout problem!我希望这能解决你的问题!

You can break the logic in 2 parts then it will be easy for you to implement.您可以将逻辑分为两部分,这样您就可以轻松实现。 - ——

let tempResult = [];

Object.values(result.data.listStudents.items).forEach((x) => {
    if (!tempResult.hasOwnProperty(x.SchoolName)) {
        tempResult[x.SchoolName] = {TotalMarks: 0, TotalCount: 0} ;
    }

    tempResult[x.SchoolName].TotalMarks = tempResult[x.SchoolName].TotalMarks + x.Marks;
    tempResult[x.SchoolName].TotalCount = tempResult[x.SchoolName].TotalCount + 1;
});

let listRec = [];
let i = 0;
Object.keys(tempResult).forEach(function (k) {
    listRec[i++] = { SchoolName: k, TotalMarks: tempResult[k].TotalMarks, TotalCount: tempResult[k].TotalCount };
});

let finalResult = { list: listRec };
console.log(finalResult);

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

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