【发布时间】:2020-04-29 21:51:17
【问题描述】:
我想得到数组中值的总和。当我console.log(this.totalCount) 使用此代码时,我只会得到这样的。
如何得到所有值的总和?
代码
return this.http
.post('/media', reqBody)
.pipe(
map((res:IStatisticReponse) => res.data)
).subscribe(res => {
this.items = res.map((r: any, i: number) => {
r.color = this.colors[i]
return r;
});
this.legendProgress = this.items.map(item => {
return { label: item.label, color: item.color };
});
this.totalCount = this.items.map((item)=> item.mediaShare);
console.log(this.totalCount)
this.isLoading = false;
});
【问题讨论】:
-
map() 方法创建了一个新数组,其中填充了在调用数组中的每个元素上调用提供的函数的结果。 所以你的代码必须是
this.totalCount.length我不知道你为什么使用空值的地图 -
@OnurGelmez 当我添加
this.items.length时出现错误(property) Array<any>.length: number Gets or sets the length of the array. This is a number one higher than the highest element defined in an array. This expression is not callable. Type 'Number' has no call signatures.ts(2349) -
console.log(this.items)的结果是什么? -
我猜,你应该说,你需要这些值的
sum -
示例:totalCount = 2656 + 1987 + 1071
标签: angular typescript