Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
Add values of objects fields in array and calculate average

I have an object UserScore. It has field called score that is an number. I am keeping all userscores in userScores variable and overallScore for average of scores.

public userScores: any = [];
public overallScore : number = 0;

I tried so far something like this:

   sumRatingValues(): void{
    let i = 0;
    this.userScores.forEach((score: any) => {
      this.overallScore += score.score;
      i++;
      console.log(this.overallScore);
    });
    this.overallScore / i;
  }

Then I am using this method in ngOnInit.

But it doesn't print to conole log anything and overallScore value is 0.

EDIT How I fetch data:

getRating(){
    this.route.params
    .subscribe((params: Params) => {
      const id = params['id'];
      this.sharedService.getRecipesScoresForRecipe(id).subscribe(scores =>{
        this.userScores = scores;
        console.log(this.userScores)
      })
  });
  }

console.log prints out properly objects with their fields and value enter image description here And my ngOnInit:

ngOnInit(): void {
    ...
    this.getRating();
    this.sumRatingValues();
}

Little testing on front:

<div class="class" *ngFor="let user of userScores">{{user.score}} - {{overallScore}}</div>

This is how it look like: result

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!