# Function to get multiple arrays

**URL:** https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457
**Category:** General
**Created:** [28 May 2020 19:52 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457 "2020-05-28T19:52:31Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![JamesMcCarthy79](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jamesmccarthy79/32/22887_2.png) [@JamesMcCarthy79](https://discourse.nodered.org/u/JamesMcCarthy79)
#### Post date: [28 May 2020 19:52 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/1 "2020-05-28T19:52:31Z")

</div>

I have a Node-RED flow that uses an API request for my cycle routes. It returns a huge array up to 2000 points each for elevation, speed & grade. I would like to pull these values out and plot them onto a ride graph. Is there a way of being able to do this with a loop function as the amount will differ per trip and I don’t fancy having to pull each one out individually.

Example

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

Path for first elevation `payload.trip.track_points[0].e`

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [28 May 2020 19:55 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/2 "2020-05-28T19:55:21Z")

</div>

@JamesMcCarthy79  
James, I assume that you have copied and pasted this post from [https://community.home-assistant.io/t/node-red-function-to-get-multiple-arrays/200024](https://community.home-assistant.io/t/node-red-function-to-get-multiple-arrays/200024)

The post above is missing the screenshot, please edit the post using the pencil icon and add your screenshot image.

---

<div class="post-metadata">

### Author: ![JamesMcCarthy79](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jamesmccarthy79/32/22887_2.png) [@JamesMcCarthy79](https://discourse.nodered.org/u/JamesMcCarthy79)
#### Post date: [28 May 2020 21:15 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/3 "2020-05-28T21:15:51Z")

</div>

@Paul-Reed I did indeed but felt maybe this was a better place to post my question.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [28 May 2020 21:18 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/4 "2020-05-28T21:18:34Z")

</div>

> [@JamesMcCarthy79](#):
>
> I did indeed but felt maybe this was a better place to post my question

Yes, I agree 😉

---

<div class="post-metadata">

### Author: ![hominidae](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hominidae/32/4174_2.png) [@hominidae](https://discourse.nodered.org/u/hominidae)
#### Post date: [29 May 2020 08:00 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/5 "2020-05-29T08:00:53Z")

</div>

...a ride graph would need a timestamp associated with each object on the array, wouldn't it?  
I don't see this in your example.

Besides, the transformation to a chart datasets can be done in a function node, using js. Lopping over the array is not a problem in there...

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [29 May 2020 08:06 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/6 "2020-05-29T08:06:24Z")

</div>

> [@hominidae](#):
>
> Lopping over the array is not a problem in there...

In that case, can you share how it's done for the OP.

---

<div class="post-metadata">

### Author: ![hominidae](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hominidae/32/4174_2.png) [@hominidae](https://discourse.nodered.org/u/hominidae)
#### Post date: [29 May 2020 08:11 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/7 "2020-05-29T08:11:07Z")

</div>

...I can share an example later, how I do that for other data from my setup.

---

<div class="post-metadata">

### Author: ![JamesMcCarthy79](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jamesmccarthy79/32/22887_2.png) [@JamesMcCarthy79](https://discourse.nodered.org/u/JamesMcCarthy79)
#### Post date: [29 May 2020 08:20 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/8 "2020-05-29T08:20:22Z")

</div>

> [@hominidae](#):
>
> ...a ride graph would need a timestamp associated with each object on the array, wouldn't it?  
> I don't see this in your example.

In the example screenshot above you can see `payload.trip.track_points[0].t` is the timestamp in question

---

<div class="post-metadata">

### Author: ![hominidae](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hominidae/32/4174_2.png) [@hominidae](https://discourse.nodered.org/u/hominidae)
#### Post date: [29 May 2020 11:14 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/9 "2020-05-29T11:14:29Z")

</div>

Ah, OK...didn't get that while browsing from my mobile 😉

I tried and quickly modify an example I use for another case, that might work for you.  
At least it should give you some hints on how this might work.  
Maybe @Paul-Reed is able to comment or improve, as I am not a JS guy 😉

```auto
let array = msg.payload.trip.track_points;
var time=0;
var elevation=0;
var speed=0;
var grade=0;

var i=0;
var chartarray = [[]];

for(let i = 0; i < array.length; i++) {
    time = array[i].t;
    elevation = array[i].e;
    speed = array[i].x;
    grade = array[i].y;
    chartarray[0][i] = {"x":time,"y":elevation};
    chartarray[1][i] = {"x":time,"y":speed};
    chartarray[2][i] = {"x":time,"y":grade};
}

var chart = [{
    "series":["Elevation", "Speed", "Grade"],
    "data":chartarray,
    "labels":[""]
}];

msg.payload = chart;
msg.topic = "trip"

return msg;

```

Info on how a dashboard chart works can be found here: [https://github.com/node-red/node-red-dashboard/blob/master/Charts.md](https://github.com/node-red/node-red-dashboard/blob/master/Charts.md)

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [29 May 2020 16:09 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/10 "2020-05-29T16:09:36Z")

</div>

> [@hominidae](#):
>
> Maybe @Paul-Reed is able to comment or improve, as I am not a JS guy

Neither am I 🤔  
Perhaps @JamesMcCarthy79 can try with his data source, and let us know if there is a problem.

---

<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: [28 July 2020 16:09 UTC](https://discourse.nodered.org/t/function-to-get-multiple-arrays/27457/11 "2020-07-28T16:09:42Z")

</div>

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