# Sum of values in an array

**URL:** <https://discourse.nodered.org/t/sum-of-values-in-an-array/74594>\
**Category:** General\
**Created:** [2 February 2023 10:02 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594 "2023-02-02T10:02:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [2 February 2023 10:02 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/1 "2023-02-02T10:02:09Z")

</div>

Hi,

I have an array like the following  
 ![array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73113ab94eb1645743ad582484a583ab2914e068.png)

I am using the function to calculate the value of Download and upload in MB. The result is coming out as a single object for each occurrence. The goal is to sum all the upload values and download values

```auto
for (var i = 0; i < msg.payload.length; i++) {
  var newMsg = {};
  uploadtype = msg.payload[i][0].upload.split(' ')[1].substr(0, 2);
  if (uploadtype == "KB") {
    uploadmultiplier = ".001";
  } else if (uploadtype == "MB") {
    uploadmultiplier = "1";
  } else if (uploadtype == "GB") {
    uploadmultiplier = "1000";
  } else {
    uploadmultiplier = "0";
  }
  uploadvalue = msg.payload[i][0].upload.split(' ')[0];

  downloadtype = msg.payload[i][0].download.split(' ')[1].substr(0, 2);
  if (downloadtype == "KB") {
    downloadmultiplier = ".001";
  } else if (downloadtype == "MB") {
    downloadmultiplier = "1";
  } else if (downloadtype == "GB") {
    downloadmultiplier = "1000";
  } else {
    downloadmultiplier = "0";
  }
  downloadvalue = msg.payload[i][0].download.split(' ')[0];

  newMsg.payload = {
    // download: msg.payload[i].PartitionKey._,
    downloadMB: downloadvalue * downloadmultiplier,
    uploadMB: uploadvalue * uploadmultiplier,
  }
  node.send(newMsg);

}

```

Here is the current output.

![Output](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fb68ddee236142e7b67d3822aa7b012a8d901c70.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 February 2023 10:59 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/2 "2023-02-02T10:59:07Z")

</div>

Well you have only provided part of your code. Looking at it, it looks like your code is doing 'what' you told it to do, not what you 'want' it to do 😂

You're not accumulating the running total of all the occurrences in the atrray.

---

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [2 February 2023 11:38 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/3 "2023-02-02T11:38:17Z")

</div>

Hi,

Yes, you are right the code is running correctly for what I asked it to do. I tried the SUM function to sum all the downloads but it's not working. It just gives me individual values without the sum. So I am not sure where I should add the sum code in above the working code.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 February 2023 11:48 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/4 "2023-02-02T11:48:10Z")

</div>

You could try a function like this:

```auto
const input = msg.payload

function getSum(i){
     const tmp = input.map(v => {
          const inputValue = v[i].split(" ")
          const sizeType = inputValue[1]
          const size = Number(inputValue[0])

          let multiplier = 0
          if (sizeType == "KB") multiplier = .001
          if (sizeType == "MB") multiplier = 1
          if (sizeType == "GB") multiplier = 1000
         
          return Number( size * multiplier )

     })
     return tmp.reduce((a, b) => a + b)
}

msg.downloadTotal = getSum("download")
msg.uploadTotal = getSum("upload")
return msg;

```

Which produces:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/235bedd48c54559c145710abf7da25b25eb76684.png)

---

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [2 February 2023 12:49 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/5 "2023-02-02T12:49:42Z")

</div>

Hi,

My array is nested. Please see below

 ![Nested2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4dc3a12d4b199c7fbcc13afdcbd07cd529d95e94.png)

Every array has an object 0 hence your code is not able to fetch values.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 February 2023 13:07 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/6 "2023-02-02T13:07:36Z")

</div>

> Every array has an object 0 hence your code is not able to fetch values.

So you modified my code to make it work ?

---

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [2 February 2023 13:08 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/7 "2023-02-02T13:08:55Z")

</div>

No, I didn't. I couldn't figure it out. Apologies for being a noob.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 February 2023 13:10 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/8 "2023-02-02T13:10:31Z")

</div>

change:

```auto
const inputValue = v[i].split(" ")

to

const inputValue = v[0][i].split(" ")

```

---

<div class="post-metadata">

**Author:** ![dineshaj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dineshaj/32/53342_2.png) [@dineshaj](https://discourse.nodered.org/u/dineshaj)\
**Post date:** [2 February 2023 13:39 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/9 "2023-02-02T13:39:07Z")

</div>

Works. Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 February 2023 13:39 UTC](https://discourse.nodered.org/t/sum-of-values-in-an-array/74594/10 "2023-02-16T13:39:31Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
