You need to enable JavaScript to run this app. 如何在Angular中对嵌套对象数组属性求和以计算各类GPA指标
优惠活动
大模型
产品
解决方案
定价
更多

如何在Angular中对嵌套对象数组属性求和以计算各类GPA指标

Angular 成绩报告单统计功能实现需求

我通过Node.js接口向Angular应用返回了名为results的数组,数组结构如下:

[
  {
    "session": "2019/2020",
    "harmattan": [
      {
        "course_code": "CSC104",
        "title": "Object Oriented Programming",
        "unit": 2,
        "status": "C",
        "result": {
          "updatedAt": "2020-12-04T11:11:54.557Z",
          "_id": "5fca1a8daac49e44a40a80e6",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      },
      {
        "course_code": "CSC105",
        "title": "Problem Solving",
        "unit": 3,
        "status": "R",
        "result": {
          "updatedAt": "2020-12-04T11:11:54.557Z",
          "_id": "5fca1aaaaac49e44a40a8105",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      }
    ],
    "rain": [
      {
        "course_code": "CSC101",
        "title": "Modelling and Simulation",
        "unit": 3,
        "status": "C",
        "result": {
          "updatedAt": "2020-12-04T11:25:38.901Z",
          "_id": "5fca1cd729eca3320c6245cc",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      },
      {
        "course_code": "CSC102",
        "title": "Introduction to Computer Science",
        "unit": 2,
        "status": "C",
        "result": {
          "updatedAt": "2020-12-04T11:25:38.901Z",
          "_id": "5fca1d2829eca3320c6245eb",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      },
      {
        "course_code": "CSC103",
        "title": "Data Structures",
        "unit": 2,
        "status": "E",
        "result": {
          "updatedAt": "2020-12-04T11:25:38.901Z",
          "_id": "5fca1d5429eca3320c62460a",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      }
    ]
  },
  {
    "session": "2020/2021",
    "harmattan": [
      {
        "course_code": "CSC104",
        "title": "Object Oriented Programming",
        "unit": 2,
        "status": "C",
        "result": {
          "updatedAt": "2020-12-04T11:11:54.557Z",
          "_id": "5fca1a8daac49e44a40a80e6",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      },
      {
        "course_code": "CSC105",
        "title": "Problem Solving",
        "unit": 3,
        "status": "R",
        "result": {
          "updatedAt": "2020-12-04T11:11:54.557Z",
          "_id": "5fca1aaaaac49e44a40a8105",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      }
    ],
    "rain": [
      {
        "course_code": "CSC101",
        "title": "Modelling and Simulation",
        "unit": 3,
        "status": "C",
        "result": {
          "updatedAt": "2020-12-04T11:25:38.901Z",
          "_id": "5fca1cd729eca3320c6245cc",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      },
      {
        "course_code": "CSC102",
        "title": "Introduction to Computer Science",
        "unit": 2,
        "status": "C",
        "result": {
          "updatedAt": "2020-12-04T11:25:38.901Z",
          "_id": "5fca1d2829eca3320c6245eb",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      },
      {
        "course_code": "CSC103",
        "title": "Data Structures",
        "unit": 2,
        "status": "E",
        "result": {
          "updatedAt": "2020-12-04T11:25:38.901Z",
          "_id": "5fca1d5429eca3320c62460a",
          "matric_no": "U19CYS1076",
          "ca": 3,
          "exam": 48,
          "total": 51,
          "point": 3,
          "grade": "C"
        }
      }
    ]
  }
]

前端核心需求

  • 展示所有已有学年的成绩,每个学年下先呈现Harmattan学期的课程内容,再呈现Rain学期的课程内容
  • 对单学期下所有课程的学分求和,得到学期总学分(TNU)
  • 对单学期下所有课程的学分绩点(课程学分*对应课程绩点)求和,得到学期总学分绩点(TCP)
  • 用学期总学分绩点除以学期总学分,计算得到当学期的GPA
  • 跨所有学期累计总学分、总学分绩点,计算全局累计总学分(CTNU)、累计总学分绩点(CTCP)、累计GPA(CGPA):累计逻辑为按时间顺序从第一个学期开始,每个学期的学分、绩点值依次叠加,比如2019/2020学年Harmattan学期的学分计算完成后,叠加同学年Rain学期的学分数值,再依次叠加后续所有学年两个学期的对应数值,绩点计算逻辑一致。

现有前端模板代码

<div *ngFor="let result of results">
  <div *ngIf="result.harmattan.length > 0">
    <table class="table table-striped">
      <thead>
        <tr>
          <th colspan="2">PROGRAM: {{userData?.department | uppercase}}</th>
          <th>ACADEMIC YEAR: {{result.session}}</th>
          <th colspan="2">SEMESTER: HARMATTAN</th>
        </tr>
        <tr class="thead-light">
          <th>COURSE CODE</th>
          <th>COURSE NAME</th>
          <th>UNIT</th>
          <th>GRADE</th>
          <th>CREDIT POINT</th>
        </tr>
      </thead>

      <tr *ngFor="let first of result.harmattan" >
        <td>{{first.course_code}}</td>
        <td>{{first.title}}</td>
        <td>{{first.unit}}</td>
        <td>{{first.result.grade}}</td>
        <td>{{first.result.point * first.unit}}</td>
      </tr>
      <p>Total Units: **value**</p>
      <p>Total Points: **value**</p>
      <p>GPA: **value**</p>
      <p>Cumulative Total Units: **value**</p>
      <p>Cumulative Total Points: **value**</p>
      <p>Cumulative GPA: **value**</p>
    </table>
  </div>
  <br>
  <div *ngIf="result.rain.length > 0">
    <table class="table table-striped">
      <thead>
        <tr>
          <th colspan="2">PROGRAM: {{userData?.department | uppercase}}</th>
          <th>ACADEMIC YEAR: {{result.session}}</th>
          <th colspan="2">SEMESTER: RAIN</th>
        </tr>
        <tr class="thead-light">
          <th>COURSE CODE</th>
          <th>COURSE NAME</th>
          <th>UNIT</th>
          <th>GRADE</th>
          <th>CREDIT POINT</th>
        </tr>
      </thead>
      <tr *ngFor="let second of result.rain">
        <td>{{second.course_code}}</td>
        <td>{{second.title}}</td>
        <td>{{second.unit}}</td>
        <td>{{second.result.grade}}</td>
        <td>{{second.result.point * second.unit}}</td>
      </tr>
      <p>Total Units: **value**</p>
      <p>Total Points: **value**</p>
      <p>GPA: **value**</p>
      <p>Cumulative Total Units: **value**</p>
      <p>Cumulative Total Points: **value**</p>
      <p>Cumulative GPA: **value**</p>
    </table>
  </div>
  <br>
</div>

统计值术语说明

  • TNU:总学分(Total Number of Units),单学期内所有课程的学分之和
  • TCP:总学分绩点(Total Credit Points),单学期内所有课程的「课程学分*对应课程绩点」的总和
  • GPA:平均学分绩点(Grade Point Average),单学期TCP除以单学期TNU的计算结果
  • CTNU:累计总学分(Cumulative Total Number of Units),跨所有学期的总学分累计值
  • CTCP:累计总学分绩点(Cumulative Total Credit Points),跨所有学期的总学分绩点累计值
  • CGPA:累计平均学分绩点(Cumulative Grade Point Average),跨所有学期的CTCP除以CTNU的计算结果

现有TypeScript组件代码

ngOnInit() {
  this.resultService.getResults()
  .subscribe(responseData => {
    this.results = responseData.results;
    console.log(this.results);
    this.userData = responseData.userData;
  });
}

内容的提问来源于stack exchange,提问作者Adedire

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 12:24:01