Sum values in an array

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

[{"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":[]}]

Here's the document Array.prototype.reduce() - JavaScript | MDN

And here's how it applies to your case

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;

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.

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;
1 Like

ok i got it. thanks for your help..

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

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

E.g.

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

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

Cheers Tommy

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