Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 获取数组中的值之和_Angular_Typescript - Fatal编程技术网

Angular 获取数组中的值之和

Angular 获取数组中的值之和,angular,typescript,Angular,Typescript,我想得到数组中的值之和。当我console.log(this.totalCount)使用此代码时,我只能得到这样的结果 如何获得所有值的总和 代码 return this.http .post('/media', reqBody) .pipe( map((res:IStatisticReponse) => res.data) ).subscribe(res => { this.items = res.map((r:

我想得到数组中的值之和。当我
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;
      });
试试这个

let totalVal;
for(let i = 0; i < this.totalCount.length; i++) {
  totalVal = totalVal + parseInt(this.totalCount[i]);
}
let totalVal;
for(设i=0;i
试试这个

let totalVal;
for(let i = 0; i < this.totalCount.length; i++) {
  totalVal = totalVal + parseInt(this.totalCount[i]);
}
let totalVal;
for(设i=0;i
在当前的实现中,您基本上迭代
数组中的所有项,并返回objects
mediaShare
属性

this.totalCount = this.items.map((item)=> item.mediaShare); // => ["123", "345", ...]
你真正想做的是得到所有这些值的总和。 考虑到
totalCount
中的值现在是似乎包含数值的
字符串的集合,您可以执行以下操作:

this.totalCount = this.items.map((item)=> Number.parseInt(item.mediaShare)).reduce((acc, curr) => acc + curr, 0); // Or `parseFloat`, if your values might be of type float

请继续阅读以了解其行为。

在当前的实现中,您基本上迭代
数组中的所有项,并返回objects
mediaShare
属性

this.totalCount = this.items.map((item)=> item.mediaShare); // => ["123", "345", ...]
你真正想做的是得到所有这些值的总和。 考虑到
totalCount
中的值现在是似乎包含数值的
字符串的集合,您可以执行以下操作:

this.totalCount = this.items.map((item)=> Number.parseInt(item.mediaShare)).reduce((acc, curr) => acc + curr, 0); // Or `parseFloat`, if your values might be of type float

继续阅读,了解它的行为。

map()方法创建一个新数组,其中填充了对调用数组中的每个元素调用所提供函数的结果。所以您的代码必须是
this.totalCount.length
即使我不知道为什么要使用带有空值的mapvalue@OnurGelmez当我添加
this.items.length
时,我得到了错误
(属性)Array.length:number获取或设置数组的长度。这是一个比数组中定义的最高元素高的数字。此表达式不可调用。类型“Number”没有呼叫签名。ts(2349)
console.log(this.items)
的结果是什么?我想,您应该说,您需要这些值的总和示例:totalCount=2656+1987+1071Map()方法创建一个新数组,其中填充了对调用数组中的每个元素调用所提供函数的结果。所以您的代码必须是
this.totalCount.length
即使我不知道为什么要使用带有空值的mapvalue@OnurGelmez当我添加
this.items.length
时,我得到了错误
(属性)Array.length:number获取或设置数组的长度。这是一个比数组中定义的最高元素高的数字。此表达式不可调用。键入“Number”没有呼叫签名。ts(2349)
console.log(this.items)
的结果是什么,您需要这些值的总和示例:totalCount=2656+1987+1071您的代码仅显示如下所示,而不是值的总和我希望它是这样的`totalCount=2656+1987+1071`这正是我给您的。我添加了另一个编辑,也许你在它之前检查了代码。请使用提供的最新解决方案再次测试。您的代码仅显示如下所示
265619871071
而不是我想要的值的总和,如'totalCount=2656+1987+1071',这正是我给您的。我添加了另一个编辑,也许你在它之前检查了代码。请使用提供的最新解决方案再次测试。