# Sum values in an array

**URL:** <https://discourse.nodered.org/t/sum-values-in-an-array/44562>\
**Category:** General\
**Created:** [22 April 2021 18:40 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562 "2021-04-22T18:40:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsadow](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@lsadow](https://discourse.nodered.org/u/lsadow)\
**Post date:** [22 April 2021 18:40 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/1 "2021-04-22T18:40:04Z")

</div>

Can someone please give me a correction on this flow...I am trying to sum the values in this example array. Thanks,

```auto
[{"id":"e9c32823.c6a048","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"2f47f00c.b8042","type":"inject","z":"e9c32823.c6a048","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":260,"wires":[["94b8caa4.e42ab8"]]},{"id":"94b8caa4.e42ab8","type":"function","z":"e9c32823.c6a048","name":"","func":"var testtable = [];\ntesttable[0] = 32455.6;\ntesttable[20] = 31399.6;\ntesttable[40] = 31299.5;\ntesttable[60] = 31280.0;\ntesttable[80] = 31270.0;\ntesttable[100] = 31200.0;\ntesttable[120] = 31155.0;\ntesttable[140] = 31100.0;\ntesttable[160] = 31075.0;\ntesttable[180] = 31000.0;\ntesttable[200] = 30900.0;\ntesttable[220] = 30800.0;\ntesttable[240] = 30700.0;\ntesttable[260] = 30600.0;\ntesttable[280] = 30550.0;\ntesttable[300] = 30485.0;\ntesttable[320] = 30400.0;\ntesttable[340] = 30300.0;\ntesttable[359] = 30100.0;\nmsg.payload = testtable;\n\nfunction sumArray(testtable) {\n for (\n var\n index = 0, // The iterator\n length = array.length, // Cache the array length\n sum = 0; // The total amount\n index < length; // The \"for\"-loop condition\n sum += array[index++] // Add number on each iteration\n );\n return sum;\n}\n\nmsg.payload = sum;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":260,"wires":[["eda574ac.c68238"]]},{"id":"eda574ac.c68238","type":"debug","z":"e9c32823.c6a048","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [22 April 2021 19:52 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/2 "2021-04-22T19:52:01Z")

</div>

Here's the document [Array.prototype.reduce() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)

And here's how it applies to your case

```auto
var testtable = [];
testtable[0] = 32455.6;
testtable[20] = 31399.6;
testtable[40] = 31299.5;
testtable[60] = 31280.0;
testtable[80] = 31270.0;
testtable[100] = 31200.0;
testtable[120] = 31155.0;
testtable[140] = 31100.0;
testtable[160] = 31075.0;
testtable[180] = 31000.0;
testtable[200] = 30900.0;
testtable[220] = 30800.0;
testtable[240] = 30700.0;
testtable[260] = 30600.0;
testtable[280] = 30550.0;
testtable[300] = 30485.0;
testtable[320] = 30400.0;
testtable[340] = 30300.0;
testtable[359] = 30100.0;

var sum = testtable.reduce((a, b) => a + b, 0)

msg.payload = sum;
return msg;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 April 2021 20:04 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/3 "2021-04-22T20:04:46Z")

</div>

Hi the answer above is the best way.

But your for loop is all messed up, it should have 3 elements.  
The array you created has a length of 360 as when adding to an array all missing indexes are added and given null values.  
You then failed to call the function and give it a value.  
Also in your function you name the incoming value to testtable but then refer to it as array.  
here is a working example so you can learn more how for loops work and testing values, in this example to confirm it is a number.

```auto
var testtable = [];
testtable[0] = 32455.6;  
testtable[20] = 31399.6;
testtable[40] = 31299.5;
testtable[60] = 31280.0;
testtable[80] = 31270.0;
testtable[100] = 31200.0;
testtable[120] = 31155.0;
testtable[140] = 31100.0;
testtable[160] = 31075.0;
testtable[180] = 31000.0;
testtable[200] = 30900.0;
testtable[220] = 30800.0;
testtable[240] = 30700.0;
testtable[260] = 30600.0;
testtable[280] = 30550.0;
testtable[300] = 30485.0;
testtable[320] = 30400.0;
testtable[340] = 30300.0;
testtable[359] = 30100.0; //this creates an array 360 in length 300+ null values
msg.payload = testtable;
node.send(msg);
function sumArray(array) {
var sum = 0;
  for (var index = 0; index < array.length; index++){ // 3 values in a for loop
        if(!isNaN(array[index])){ // check if value is a number
            sum += array[index] // Add number on each iteration
        }
      }
  return sum;
}

msg.payload = sumArray(msg.payload) // send value to function
return msg;

```

---

<div class="post-metadata">

**Author:** ![lsadow](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@lsadow](https://discourse.nodered.org/u/lsadow)\
**Post date:** [22 April 2021 23:27 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/4 "2021-04-22T23:27:11Z")

</div>

ok i got it. thanks for your help..

---

<div class="post-metadata">

**Author:** ![Tommy1](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@Tommy1](https://discourse.nodered.org/u/Tommy1)\
**Post date:** [17 May 2021 19:51 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/5 "2021-05-17T19:51:00Z")

</div>

Hi I'm having kind of the same problem I have an array im wanting to sum, value [0] . [1] . [2] . I want to add 0 , 1 and 2 together but when I use function  
Var one = msg payload [0];  
Var two = msg.payload [1];  
Var two = msg.payload [2];

Var total = one + two + three;  
Msg.payload = total;

The result im getting is the values of the arrays next to each other ? If I replace the +with\* the numbers are multiplied and the correct answer is resulting.  
Sorry if I sound stupid but my background is plc ladder programming and I'm just breaking in to java.

Cheers Tommy

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 May 2021 20:04 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/6 "2021-05-17T20:04:47Z")

</div>

> [@Tommy1](#):
>
> The result im getting is the values of the arrays next to each other

That suggests the values in the array are strings. Wrap the values with `Number()`

E.g.

```auto
var one = Number(msg payload[0]);

```

---

<div class="post-metadata">

**Author:** ![Tommy1](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@Tommy1](https://discourse.nodered.org/u/Tommy1)\
**Post date:** [18 May 2021 07:34 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/7 "2021-05-18T07:34:52Z")

</div>

Hi @Steve-Mcl  
Thanks mate that worked a treat your a gent.

Cheers Tommy

---

<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:** [17 July 2021 07:35 UTC](https://discourse.nodered.org/t/sum-values-in-an-array/44562/8 "2021-07-17T07:35:15Z")

</div>

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