0

我想读取我的数组的每个速率属性。我不知道有什么方法可以只从该费率记录中调用 $values 吗?

在此处输入图像描述

我这样做是为了调用这个数组:

async ngOnInit() {
    this.product$ = await this.reviewService.getReview(this.product.$key);
    this.key = Object.values(this.product.reviews);
}
4

1 回答 1

2

因此,您可以使用 map 方法获取一组费率,然后使用 reduce,我的意思是:

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
}
于 2018-03-16T01:46:55.127 回答