1

I want to read every rate property of my array. I don't know is there any method to call only $values from that rate record ??

enter image description here

I did this to call this array:

async ngOnInit() {
    this.product$ = await this.reviewService.getReview(this.product.$key);
    this.key = Object.values(this.product.reviews);
}
2
  • Do u want the total of rates? Commented Mar 16, 2018 at 1:28
  • actually i'm going to sum this, so yes i need iterate by values of rates and sum it to variable Commented Mar 16, 2018 at 1:32

1 Answer 1

4

So you can use the map method to get an array of rates and then use reduce, I mean:

ngOnInit():void{
    // get an array of rates 
    const rates = yourArray.map(item => item.rate) // rates=[2,3,5,1,..]

    // use reduce function to calculate the sum
    const sum = rates.reduce(this.total)
} 



private total(total,num){
   return total + num
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.