1

I want to get sum of the values in array. With this code when I console.log(this.totalCount) I only get like this enter image description here.

How to get sum of all the values?

CODE

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;
      });
14
  • The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. so you code must be this.totalCount.length even I not sure why you are using map with empty value Commented Jan 13, 2020 at 7:39
  • @OnurGelmez when I add this.items.length I got error (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) Commented Jan 13, 2020 at 7:43
  • what is the result of console.log(this.items)? Commented Jan 13, 2020 at 7:44
  • 2
    I guess, you should say, you need sum of those value Commented Jan 13, 2020 at 7:48
  • 1
    example: totalCount = 2656 + 1987 + 1071 Commented Jan 13, 2020 at 7:48

2 Answers 2

3

In your current implementation you basically iterate over all items within the items array and return the objects mediaShare property.

this.totalCount = this.items.map((item)=> item.mediaShare); // => ["123", "345", ...]

What you actually want to do is to get the sum of all these values. Considering the values inside totalCount are now a collection of strings that seem to hold a numeric value, you can do the following:

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

Read up on Array.prototype.reduce to learn how it behaves.

Sign up to request clarification or add additional context in comments.

3 Comments

Your code only display like this 265619871071 not sum of the value
I want it like this ` totalCount = 2656 + 1987 + 1071 `
Which is exactly what I gave you. I added another edit, maybe you checked the code prior to it. Please test it again with the latest solution provided.
1

try this

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.