arraysangulartypescriptobject-property

angular 4 - read property from an array of objects


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);
}

Solution

  • 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
    }