# Summing array elements

**URL:** <https://discourse.nodered.org/t/summing-array-elements/37988>\
**Category:** General\
**Created:** [24 December 2020 21:05 UTC](https://discourse.nodered.org/t/summing-array-elements/37988 "2020-12-24T21:05:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FatPilot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatpilot/32/38329_2.png) [@FatPilot](https://discourse.nodered.org/u/FatPilot)\
**Post date:** [24 December 2020 21:05 UTC](https://discourse.nodered.org/t/summing-array-elements/37988/1 "2020-12-24T21:05:22Z")

</div>

Hi everyone and Merry Christmas!  
I'm trying to average some gps lat and long positions to smooth the output. the array is limited to two for testing purposes and would normally be in the order of 100.  
The two lines fail to add correctly in the FOR loop and when I look at the values for smoothed\_lat or smoothed\_lon I get NaN. it seems the problem is adding the values. assigning an array element to another variable works as I have shown.  
I know these are floating point numbers, am I expecting too much?  
I know the values are correct in the array - position\_array as can be seen.

Really struggling with this.

Cheers.  
Paul.

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

```
// average the position to smooth any fluctuations
if (position_array.length == 2) {
    var junk = position_array.shift();
    position_array.push([float_lat, float_lon]);
} else {
    position_array.push([float_lat, float_lon]);
}

var smoothed_lat = 0;
var smoothed_lon = 0;

for (var loop = 0; loop <= position_array.length; loop++){
        smoothed_lat = smoothed_lat + position_array[(loop)[0]]; // This fails
        smoothed_lon = smoothed_lon + position_array[(loop)[1]]; // This fails
}

smoothed_lat = position_array[0][0]; // This works

smoothed_lat = smoothed_lat / position_array.length; // gets the average
smoothed_lon = smoothed_lon / position_array.length; // gets the average
```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 December 2020 21:41 UTC](https://discourse.nodered.org/t/summing-array-elements/37988/3 "2020-12-24T21:41:27Z")

</div>

> [@FatPilot](#):
>
> `for (var loop = 0; loop <= position_array.length; loop++){`

If the length is 2 that will run through the loop with indices 0, 1, and 2, so you are going off the end of the array. It should be

```auto
for (var loop = 0; loop < position_array.length; loop++){

```

Is the bit at the front with the shift and push in some way related to taking the average? I don't see what it is for.

---

<div class="post-metadata">

**Author:** ![FatPilot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatpilot/32/38329_2.png) [@FatPilot](https://discourse.nodered.org/u/FatPilot)\
**Post date:** [24 December 2020 21:46 UTC](https://discourse.nodered.org/t/summing-array-elements/37988/4 "2020-12-24T21:46:44Z")

</div>

Hi Colin,  
The bit at the front is showing the array being filled and is not related to the averaging.

However thanks for your reply but it still fails with NaN. ☹

Cheers. Paul.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 December 2020 22:04 UTC](https://discourse.nodered.org/t/summing-array-elements/37988/5 "2020-12-24T22:04:24Z")

</div>

```auto
position_array[(loop)[0]]

```

I think that should be

```auto
position_array[loop][0]

```

If that doesn't fix it have a look at the node-red docs page Writing Functions which shows how you can use `node.warn()` to show values as the function is executed. That should enable you to work out what the problem is.

---

<div class="post-metadata">

**Author:** ![FatPilot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatpilot/32/38329_2.png) [@FatPilot](https://discourse.nodered.org/u/FatPilot)\
**Post date:** [24 December 2020 22:11 UTC](https://discourse.nodered.org/t/summing-array-elements/37988/6 "2020-12-24T22:11:50Z")

</div>

Hi Colin, Yes that's cracked it!  
Two silly mistakes.

Merry Christmas!

Cheers.  
Paul.

---

<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:** [24 December 2020 23:16 UTC](https://discourse.nodered.org/t/summing-array-elements/37988/7 "2020-12-24T23:16:25Z")

</div>

The reduce method would work to  
eg.

```auto
let smoothed = msg.payload.reduce( (accumulator, currentValue, index, array) => ([accumulator[0] + currentValue[0], accumulator[1] + currentValue[1]] ));
let length = msg.payload.length;
msg.smoothed = [(smoothed[0]/length), (smoothed[1]/length)];
return msg;

```

Here is an example flow

```auto
[{"id":"517fe8ab.2c4da","type":"function","z":"8d22ae29.7df6d","name":"","func":"let smoothed = msg.payload.reduce( (accumulator, currentValue, index, array) => ([accumulator[0] + currentValue[0], accumulator[1] + currentValue[1]] ));\nlet length = msg.payload.length;\nmsg.smoothed = [(smoothed[0]/length), (smoothed[1]/length)];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":2280,"wires":[["84c9d74a.048ca8"]]},{"id":"62276cee.8861cc","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[52.5673342345,1.2945556785],[51.3458652378,1.3456737106],[51.1234560076,1.1111234001]]","payloadType":"json","x":170,"y":2300,"wires":[["517fe8ab.2c4da"]]},{"id":"84c9d74a.048ca8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":500,"y":2280,"wires":[]}]

```

---

<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:** [7 January 2021 23:16 UTC](https://discourse.nodered.org/t/summing-array-elements/37988/8 "2021-01-07T23:16:35Z")

</div>

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