Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用AngularJs对对象数组中的属性值求和_Javascript_Html_Arrays_Angularjs_Typescript - Fatal编程技术网

Javascript 如何使用AngularJs对对象数组中的属性值求和

Javascript 如何使用AngularJs对对象数组中的属性值求和,javascript,html,arrays,angularjs,typescript,Javascript,Html,Arrays,Angularjs,Typescript,因此,我从nodejsapi到我的angular应用程序中获得了一系列结果,结果如下 [ { "session": "2019/2020", "harmattan": [ { "course_code": "CSC104", "title": "Object Oriented Programming", "unit": 2, "statu

因此,我从nodejsapi到我的angular应用程序中获得了一系列结果,结果如下

[ { "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" } } ] } ] [ { “会议”:“2019/2020年”, “哈马坦”:[ { “课程代码”:“CSC104”, “标题”:“面向对象编程”, “单位”:2, “地位”:“C”, “结果”:{ “更新日期”:“2020-12-04T11:11:54.557Z”, “_id”:“5fca1a8daac49e44a40a80e6”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } }, { “课程代码”:“CSC105”, “标题”:“问题解决”, “单位”:3, “状态”:“R”, “结果”:{ “更新日期”:“2020-12-04T11:11:54.557Z”, “_id”:“5fca1aaaaac49e44a40a8105”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } } ], “雨”:[ { “课程代码”:“CSC101”, “标题”:“建模与仿真”, “单位”:3, “地位”:“C”, “结果”:{ “更新日期”:“2020-12-04T11:25:38.901Z”, “_id”:“5fca1cd729eca3320c6245cc”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } }, { “课程代码”:“CSC102”, “标题”:“计算机科学导论”, “单位”:2, “地位”:“C”, “结果”:{ “更新日期”:“2020-12-04T11:25:38.901Z”, “_id”:“5fca1d2829eca3320c6245eb”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } }, { “课程代码”:“CSC103”, “标题”:“数据结构”, “单位”:2, “地位”:“E”, “结果”:{ “更新日期”:“2020-12-04T11:25:38.901Z”, “_id”:“5fca1d5429eca3320c62460a”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } } ] }, { “会议”:“2020/2021年”, “哈马坦”:[ { “课程代码”:“CSC104”, “标题”:“面向对象编程”, “单位”:2, “地位”:“C”, “结果”:{ “更新日期”:“2020-12-04T11:11:54.557Z”, “_id”:“5fca1a8daac49e44a40a80e6”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } }, { “课程代码”:“CSC105”, “标题”:“问题解决”, “单位”:3, “状态”:“R”, “结果”:{ “更新日期”:“2020-12-04T11:11:54.557Z”, “_id”:“5fca1aaaaac49e44a40a8105”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } } ], “雨”:[ { “课程代码”:“CSC101”, “标题”:“建模与仿真”, “单位”:3, “地位”:“C”, “结果”:{ “更新日期”:“2020-12-04T11:25:38.901Z”, “_id”:“5fca1cd729eca3320c6245cc”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } }, { “课程代码”:“CSC102”, “标题”:“计算机科学导论”, “单位”:2, “地位”:“C”, “结果”:{ “更新日期”:“2020-12-04T11:25:38.901Z”, “_id”:“5fca1d2829eca3320c6245eb”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } }, { “课程代码”:“CSC103”, “标题”:“数据结构”, “单位”:2, “地位”:“E”, “结果”:{ “更新日期”:“2020-12-04T11:25:38.901Z”, “_id”:“5fca1d5429eca3320c62460a”, “矩阵编号”:“U19CYS1076”, “ca”:3, “考试”:48, “总数”:51, "点":三,, “等级”:“C” } } ] } ] 现在,我打算在我的前端实现以下目标:

  • 显示每个可用课程的结果,Harmattan学期排在第一位,Rain学期紧随其后
  • 我想把每学期课程的所有单元加起来(TNU)
  • 我想把每学期每门课程的分数加起来(TCP)
  • 然后我想把第三名(总分)的结果除以第二名(总单位)得到平均成绩
  • 现在将保留第3和第4项的结果,并将其添加到其他学期结果中,从而得出累积值,即CTNU、CTCP、CGPA。也就是说,哈马坦2019/2020学期的总单元将被添加到2019/2020学期和其他课程中。分数也一样
  • 现在让我向您展示我的前端代码的外观

    <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>
                <pCumulative 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>
                <pCumulative GPA: **value**</p>
    
              </table>
            </div>
            <br>
          </div>
    
    
    ngOnInit() {
            this.resultService.getResults()
            .subscribe(responseData => {
              this.results = responseData.results;
              console.log(this.results);
              this.userData = responseData.userData;
            });
          }
    
    <div *ngFor="let result of resultData; let i = index">
    <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: {{ totalUnits(result.harmattan) }}</p>
                <p>Total Points: {{ totalPoints(result.harmattan) }}</p>
                <p>GPA: {{ totalGPA(result.harmattan) }}</p>
                <p>Cumulative Total Units: {{cumulativeUnits(result.harmattan, i, 'harmattan')}}</p>
                <p>Cumulative Total Points: {{cumulativePoints(result.harmattan, i, 'harmattan')}}</p>
                <p> Cumulative GPA: {{cumulativeGPA(result.harmattan, i, 'harmattan')}}</p>
              </table>
            </div>
            <br>
            <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: {{ totalUnits(result.rain) }}</p>
                <p>Total Points: {{ totalPoints(result.rain) }}</p>
                <p>GPA: {{ totalGPA(result.rain) }}</p>
                <p>Cumulative Total Units: {{cumulativeUnits(result.rain, i, 'rain')}}</p>
                  <p>Cumulative Total Points: {{cumulativePoints(result.rain, i, 'rain')}}</p>
                  <p>Cumulative GPA: {{cumulativeGPA(result.rain, i, 'rain')}}</p>
      
                </table>
              </div>
    
    cumulativeUnits(result, session, semester){
    let tnu = 0
    var tnuHarmattan = 0;
    let thisSemester = 0;
    if(session == 0){
      if (semester == 'harmattan'){
        for(let item of this.resultData[0].harmattan){
          tnuHarmattan += item.unit;
        }
        return tnuHarmattan;
      }
      else if (semester == 'rain'){
        for(let item of this.resultData[0].harmattan){
          tnuHarmattan += item.unit;
        }
        for(let item of this.resultData[0].rain){
          tnu += item.unit;
        }
        return tnuHarmattan + tnu;
      }
    }
    else{
      if (semester == 'harmattan'){
        for(let i:number = 0; i < session; i++){
      
          for(let item of this.resultData[i].harmattan){
            tnuHarmattan += item.unit;
          }
          for(let item of this.resultData[i].rain){
            tnu += item.unit;
          }
          for(let item of result){
            thisSemester += item.unit;
          }
        }
        return tnuHarmattan + tnu + thisSemester;
      }
      else if (semester == 'rain'){
        for(let i:number = 0; i <= session; i++){
      
          for(let item of this.resultData[i].harmattan){
            tnuHarmattan += item.unit;
          }
          for(let item of this.resultData[i].rain){
            tnu += item.unit;
          }
        }
        return tnuHarmattan + tnu;
      }
      
    }
    
    cumulativePoints(result, session, semester){
    let gpSemester = 0;
    let guyCPRain = 0;
    let guyCPharmattan = 0;
    if(session == 0){
      if (semester == 'harmattan'){
        for(let item of this.resultData[0].harmattan){
          guyCPharmattan += (item.result.point * item.unit);
        }
        return guyCPharmattan;
      }
      else if (semester == 'rain'){
        for(let item of this.resultData[0].harmattan){
          guyCPharmattan += (item.result.point * item.unit);
        }
        for(let item of this.resultData[0].rain){
          guyCPRain += (item.result.point * item.unit);
        }
        return guyCPharmattan + guyCPRain;
      }
    }
    else{
      if (semester == 'harmattan'){
        for(let i:number = 0; i < session; i++){
      
          for(let item of this.resultData[i].harmattan){
            guyCPharmattan += (item.result.point * item.unit);
          }
          for(let item of this.resultData[i].rain){
            guyCPRain += (item.result.point * item.unit);
          }
          for(let item of result){
            gpSemester +=  (item.result.point * item.unit);
          }
        }
        return guyCPharmattan + guyCPRain + gpSemester;
      }
      else if (semester == 'rain'){
        for(let i:number = 0; i <= session; i++){
      
          for(let item of this.resultData[i].harmattan){
            guyCPharmattan += (item.result.point * item.unit);
          }
          for(let item of this.resultData[i].rain){
            guyCPRain += (item.result.point * item.unit);
          }
        }
        return guyCPharmattan + guyCPRain;
      }
      
    }
    
    cumulativeGPA(result, session, semester){
    let tnu = 0
    var tnuHarmattan = 0;
    let thisSemester = 0;
    let guyCPRain = 0;
    let guyCPharmattan = 0;
    let gpSemester = 0;
    if(session == 0){
      if (semester == 'harmattan'){
        for(let item of this.resultData[0].harmattan){
          guyCPharmattan += (item.result.point * item.unit);
          tnuHarmattan += item.unit;
        }
        return guyCPharmattan / tnuHarmattan;
      }
      else if (semester == 'rain'){
        for(let item of this.resultData[0].harmattan){
          guyCPharmattan += (item.result.point * item.unit);
          tnuHarmattan += item.unit;
        }
        for(let item of this.resultData[0].rain){
          guyCPRain += (item.result.point * item.unit);
          tnu += item.unit;
        }
        return ((guyCPharmattan + guyCPRain) / (tnuHarmattan + tnu)).toFixed(2);
      }
    }
    else{
      if (semester == 'harmattan'){
        for(let i:number = 0; i < session; i++){
      
          for(let item of this.resultData[i].harmattan){
            guyCPharmattan += (item.result.point * item.unit);
            tnuHarmattan += item.unit;
          }
          for(let item of this.resultData[i].rain){
            guyCPRain += (item.result.point * item.unit);
            tnu += item.unit;
          }
          for(let item of result){
            thisSemester += item.unit;
            gpSemester +=  (item.result.point * item.unit);
          }
        }
        return ((guyCPharmattan + guyCPRain + gpSemester) / (tnuHarmattan + tnu + thisSemester)).toFixed(2);;
      }
      else if (semester == 'rain'){
        for(let i:number = 0; i <= session; i++){
      
          for(let item of this.resultData[i].harmattan){
            guyCPharmattan += (item.result.point * item.unit);
            tnuHarmattan += item.unit;
          }
          for(let item of this.resultData[i].rain){
            guyCPRain += (item.result.point * item.unit);
            tnu += item.unit;
          }
        }
        return ((guyCPharmattan + guyCPRain) / (tnuHarmattan + tnu)).toFixed(2);
      }
      
    }