# Function Arrays from Database

**URL:** <https://discourse.nodered.org/t/function-arrays-from-database/13949>\
**Category:** General\
**Created:** [2 August 2019 21:13 UTC](https://discourse.nodered.org/t/function-arrays-from-database/13949 "2019-08-02T21:13:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [2 August 2019 21:13 UTC](https://discourse.nodered.org/t/function-arrays-from-database/13949/1 "2019-08-02T21:13:19Z")

</div>

Hello All  
I am having a bit of trouble with some arrays within a function node. Currently I have a mysql database that returns the last 7 days worth of data. It returns it all within an array. See here  
 ![stats](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bb1681c7ff3ba338864eefcd284e9c6a8caf25e8.jpeg)

After it returns the array. I would like to take all wheel rotations and add it together. Simple right? yeah probably for most. I have done it in php but not node red.  
Brushing up on my JS in the function I was trying to use the forEach seen below. it does not appear to like this  
[https://www.w3schools.com/js/js\_array\_iteration.asp](https://www.w3schools.com/js/js_array_iteration.asp)

here is the code I have now which is def wrong, I am used to the array [0] being at the end not the middle and I don't know what to do with it. I need to itterate through it hence the foreach but I am failing obviously

```auto
var p0 = msg.payload[0].wheel_rotations ;
p2.forEach(test);
function test(test4)
{
    var test2 = test4 + test2;
}

```

Any suggestions?

---

<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 August 2019 21:22 UTC](https://discourse.nodered.org/t/function-arrays-from-database/13949/2 "2019-08-02T21:22:38Z")

</div>

I'm loving jsonata more and more  
add this change node after the mysql node and see how it works

```auto
[{"id":"35cd9577.751322","type":"change","z":"71c2416a.1852f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload.wheel_rotations)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":160,"wires":[["8c178f60.66d108"]]}]

```

---

<div class="post-metadata">

**Author:** ![biz](https://avatars.discourse-cdn.com/v4/letter/b/848f3c/32.png) [@biz](https://discourse.nodered.org/u/biz)\
**Post date:** [2 August 2019 21:28 UTC](https://discourse.nodered.org/t/function-arrays-from-database/13949/3 "2019-08-02T21:28:21Z")

</div>

> [@zenofmud](#):
>
> ```auto
> [{"id":"35cd9577.751322","type":"change","z":"71c2416a.1852f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload.wheel_rotations)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":160,"wires":[["8c178f60.66d108"]]}]
> 
> ```

oh myyyyyyyyyyy god that was so dang easy. I was making it 100x more complicated. I so need to learn more of this jsonata . that is EXACTLY what I need sooo easy.  
I will keep this in mind for the rest of my nodes that I will be doing this to and converting some old ones. 🙂

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [2 August 2019 21:50 UTC](https://discourse.nodered.org/t/function-arrays-from-database/13949/4 "2019-08-02T21:50:23Z")

</div>

It was also easy to do in the query `SELECT Sum('wheel_rotations'), (date(now())-Interval 7(day) as diff...........etc`
