# Trying to summate array

**URL:** <https://discourse.nodered.org/t/trying-to-summate-array/51057>\
**Category:** Dashboard\
**Created:** [15 September 2021 13:23 UTC](https://discourse.nodered.org/t/trying-to-summate-array/51057 "2021-09-15T13:23:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [15 September 2021 13:23 UTC](https://discourse.nodered.org/t/trying-to-summate-array/51057/1 "2021-09-15T13:23:22Z")

</div>

Hello  
I have the following payload and I am trying to summate all the used: items

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

My code is:

> var total=0;  
> var i=0;
> 
> for (i=0; i\< msg.payload.ScannedCount; i++) {
> 
> total = total + msg.payload.Items[i].used;  
> }
> 
> msg.payload = total;  
> return msg;

But I keep getting 0?

---

<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:** [15 September 2021 13:33 UTC](https://discourse.nodered.org/t/trying-to-summate-array/51057/2 "2021-09-15T13:33:23Z")

</div>

> [@Sirhc](#):
>
> ScannedCount

The length on an array in JavaScript is accessed via `.length` not `.ScannedCount`

> [@Sirhc](#):
>
> msg.payload.Items[i].used

There is also no `Items` property in a JavaScript array.

`msg.payload[i].used` is how you would access the `used` property in the array element object.

Reference: [Array - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)

---

<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:** [15 September 2021 14:28 UTC](https://discourse.nodered.org/t/trying-to-summate-array/51057/3 "2021-09-15T14:28:14Z")

</div>

You can use the Array.reduce() function. Something like this.

```auto
msg.payload = msg.payload.reduce( accumulate, 0)
return msg;

// add this value to the total
function accumulate(total, thisOne) {
    return total + thisOne.used
}

```

See [JavaScript Array reduce() Method](https://www.w3schools.com/jsref/jsref_reduce.asp)

---

<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:** [15 September 2021 17:20 UTC](https://discourse.nodered.org/t/trying-to-summate-array/51057/4 "2021-09-15T17:20:15Z")

</div>

You can do this in a change node to, using JSONata. Feed your array into the change node to see the result.

`$sum($$.payload.used)`

```auto
[{"id":"8b46158f.df50e8","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"used\":2},{\"used\":4}]","payloadType":"json","x":460,"y":3820,"wires":[["3b5365c7.3e3092"]]},{"id":"3b5365c7.3e3092","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum($$.payload.used)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":3820,"wires":[["a76897c9.a113f8"]]},{"id":"a76897c9.a113f8","type":"debug","z":"b779de97.b1b46","name":"","active":true,"console":"false","complete":"false","x":899,"y":3740,"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:** [15 October 2021 17:20 UTC](https://discourse.nodered.org/t/trying-to-summate-array/51057/5 "2021-10-15T17:20:35Z")

</div>

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