# Function to go through an array of arrays?

**URL:** <https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567>\
**Category:** General\
**Created:** [30 September 2020 12:56 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567 "2020-09-30T12:56:11Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)\
**Post date:** [1 October 2020 15:29 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/21 "2020-10-01T15:29:26Z")

</div>

Simply put, how do you add 1 to an index?

How do you translate this `brightness = brightness + 1;` to an array where you say I know I had for example rgbPairs[0] last time so make it rgbPairs[1] now and on the second "bang" make it rgbPairs[2]? Then restart from rgbPairs[0]?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [1 October 2020 15:36 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/22 "2020-10-01T15:36:05Z")

</div>

As for brightness case you have incoming data and you can take it as base value, change it and outgoing message can have that changed value.

Do you have incoming value for that rgb\_color? If so, which kind of data it is? Is it array with 3 values?

---

<div class="post-metadata">

**Author:** ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)\
**Post date:** [1 October 2020 15:47 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/23 "2020-10-01T15:47:46Z")

</div>

Yes. That is correct.

This would be a simple code where the rgb\_color data would just pass through

```auto
msg.payload= {
 domain: "light",
 service: "turn_on",
 data: {
 entity_id: "light.target_light",
 rgb_color: msg.data.attributes.rgb_color
 }
};

return msg;

```

---

<div class="post-metadata">

**Author:** ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)\
**Post date:** [1 October 2020 15:50 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/24 "2020-10-01T15:50:52Z")

</div>

Now I have this, but as it is, it will reset to 0 whenever new info is received. I was thinking for loops but from my understanding, for loops are going to give me all the available subarrays in one go.

And it also does not limit the index from 0 to 2.

I did not test the code below yet

```auto
// Create an array of strings
rgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181]];

var arrayIndex = 0;

let result = rgbPairs[arrayIndex];

arrayIndex = arrayIndex + 1;

msg.payload = {
 domain: "light",
 service: "turn_on",
 data: {
 entity_id: "light.lab_color_bulb",
 rgb_color:result
 }
};
return msg;

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [1 October 2020 15:57 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/25 "2020-10-01T15:57:32Z")

</div>

That index must be managed to be in array boundaries and must be stored in context.

> [@ygreq](#):
>
> ```auto
> // Create an array of strings
> rgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181]];
> 
> let rgbindex = context.get('rgbindex')
> if(!rgbindex){
> rgbindex = rgbPairs.length 
> }
> rgbindex ++
> if(rgbindex > rgbPairs.length){
> rgbindex = 1
> }
> 
> context.set('rgbindex',rgbindex);
> 
> let result = rgbPairs[rgbindex -1];
> 
> msg.payload = {
> domain: "light",
> service: "turn_on",
> data: {
> entity_id: "light.lab_color_bulb",
> rgb_color:result
> }
> };
> return msg;
> 
> ```

---

<div class="post-metadata">

**Author:** ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)\
**Post date:** [1 October 2020 18:54 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/26 "2020-10-01T18:54:45Z")

</div>

OMG!!!!

I really have to learn how to use context.set  
Thank you so much!!

---

<div class="post-metadata">

**Author:** ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)\
**Post date:** [1 October 2020 19:07 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/27 "2020-10-01T19:07:42Z")

</div>

Tomorrow I will try to understand what is going on. There are lots of things to learn from this.

Thank you!!

---

<div class="post-metadata">

**Author:** ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)\
**Post date:** [2 October 2020 08:03 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/28 "2020-10-02T08:03:52Z")

</div>

Ok. I believe I got it. This is how the code looks like for when I want to go backwards on the array. I put a flow.get and flow.set instead of context.set and get so that the 2 function flows can communicate.

Thank you so so much for all your help!!!

```auto

// Add an array of arrays
rgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181], [241, 236, 206], [245, 250, 246], 
            [231, 136, 52], [228, 145, 175], [231, 190, 220], [213, 228, 75], [235, 246, 252], 
            [230, 115, 70], [217, 51, 123], [201, 132, 187], [168, 214, 43], [220, 240, 247],
            [218, 93, 65], [220, 74, 49], [143, 38, 133], [75, 65, 138], [108, 131, 185]];

// define variable that holds current set (array index) (if doesn't exist, make it the lenght of the array)     
let rgbindex = flow.get('rgbindex')
if(!rgbindex){
    rgbindex = rgbPairs.length 
}

// add 1 to the index
rgbindex ++
// if index is > then the lenght of the array, then make index = 1
if(rgbindex > rgbPairs.length){
    rgbindex = 1
}

// update the current set with the new value of the index
flow.set('rgbindex',rgbindex);

// substract 1 from index so it is from 0 to index-1
let result = rgbPairs[rgbindex -1];

// create payload to include the new rgb color
msg.payload = {
 domain: "light",
 service: "turn_on",
 data: {
 entity_id: "light.lab_color_bulb",
 rgb_color: result
 }
};
return msg;

```

```auto
[{"id":"e206a6b6.416668","type":"inject","z":"7503cb14.1c6134","name":"color switch right","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":340,"y":1200,"wires":[["9c611585.784688"]]},{"id":"7dc4784b.6f2608","type":"inject","z":"7503cb14.1c6134","name":"color switch left","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":340,"y":1140,"wires":[["77e09144.04c91"]]},{"id":"77e09144.04c91","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":1040,"wires":[["77c075dc.9fe4dc","c0678e0b.382d6"]]},{"id":"9c611585.784688","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":1160,"wires":[["290bf86c.154fc8"]]},{"id":"c0678e0b.382d6","type":"function","z":"7503cb14.1c6134","name":"Color Switch Left","func":"\n// Add an array of arrays\nrgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181], [241, 236, 206], [245, 250, 246], \n [231, 136, 52], [228, 145, 175], [231, 190, 220], [213, 228, 75], [235, 246, 252], \n [230, 115, 70], [217, 51, 123], [201, 132, 187], [168, 214, 43], [220, 240, 247],\n [218, 93, 65], [220, 74, 49], [143, 38, 133], [75, 65, 138], [108, 131, 185]];\n\n// define variable that holds current set (array index) (if doesn't exist, make it the lenght of the array) \nlet rgbindex = flow.get('rgbindex')\nif(!rgbindex){\n rgbindex = rgbPairs.length \n}\n\n// substract 1 from the index\nrgbindex --\n// if index is < then 1, then make the lenght of the array\nif(rgbindex < 1){\n rgbindex = rgbPairs.length\n}\n\n// update the current set with the new value of the index\nflow.set('rgbindex',rgbindex);\n\n// substract 1 from index so it is from 0 to index-1\nlet result = rgbPairs[rgbindex -1];\n\n// create payload to include the new rgb color\nmsg.payload = {\n domain: \"light\",\n service: \"turn_on\",\n data: {\n entity_id: \"light.lab_color_bulb\",\n rgb_color: result\n }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":1000,"wires":[["754568a7.1ee2d8","8e4932b4.e3656"]],"info":"// Create an array of arrays\nrgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181]];\n\nvar arrayIndex = 0;\n\nlet result = rgbPairs[arrayIndex];\n\narrayIndex = arrayIndex + 1;\nprocessedArrayIndex = arrayIndex;\n\nmsg.payload = {\n domain: \"light\",\n service: \"turn_on\",\n data: {\n entity_id: \"light.lab_color_bulb\",\n rgb_color:result\n }\n};\nreturn msg;"},{"id":"290bf86c.154fc8","type":"function","z":"7503cb14.1c6134","name":"Color Switch Right","func":"\n// Add an array of arrays\nrgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181], [241, 236, 206], [245, 250, 246], \n [231, 136, 52], [228, 145, 175], [231, 190, 220], [213, 228, 75], [235, 246, 252], \n [230, 115, 70], [217, 51, 123], [201, 132, 187], [168, 214, 43], [220, 240, 247],\n [218, 93, 65], [220, 74, 49], [143, 38, 133], [75, 65, 138], [108, 131, 185]];\n\n// define variable that holds current set (array index) (if doesn't exist, make it the lenght of the array) \nlet rgbindex = flow.get('rgbindex')\nif(!rgbindex){\n rgbindex = rgbPairs.length \n}\n\n// add 1 to the index\nrgbindex ++\n// if index is > then the lenght of the array, then make index = 1\nif(rgbindex > rgbPairs.length){\n rgbindex = 1\n}\n\n// update the current set with the new value of the index\nflow.set('rgbindex',rgbindex);\n\n// substract 1 from index so it is from 0 to index-1\nlet result = rgbPairs[rgbindex -1];\n\n// create payload to include the new rgb color\nmsg.payload = {\n domain: \"light\",\n service: \"turn_on\",\n data: {\n entity_id: \"light.lab_color_bulb\",\n rgb_color: result\n }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":1160,"wires":[["754568a7.1ee2d8","8e4932b4.e3656"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"8e4932b4.e3656","type":"debug","z":"7503cb14.1c6134","name":"complete","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1420,"y":1040,"wires":[]},{"id":"754568a7.1ee2d8","type":"api-call-service","z":"7503cb14.1c6134","name":"Light Brightness","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1200,"y":1040,"wires":[[]],"info":"{\"brightness\": {{data.attributes.brightness}}}"},{"id":"3ed2660b.f6d8ba","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [2 October 2020 08:37 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/29 "2020-10-02T08:37:32Z")

</div>

There is no reason to make multiple copies of same function.  
To use same function for going forwards or backwards, the incoming payload should carry some kind of property which tells to even add 1 to index or subtract 1 from index. Also the "keep index in array boundaries" functionality needs the to be smarter a bit.

As the payload is coming from custom node I don't know what it is. But you can put debug node just before the function node and see what is the payload, is there anything to make such decisions or it needs to be added somehow to the incoming payload.

---

<div class="post-metadata">

**Author:** ![ygreq](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ygreq/32/28844_2.png) [@ygreq](https://discourse.nodered.org/u/ygreq)\
**Post date:** [5 October 2020 13:04 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/30 "2020-10-05T13:04:51Z")

</div>

I will try that for sure. Thank you very very much for all your help!

---

<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:** [4 December 2020 13:04 UTC](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567/31 "2020-12-04T13:04:58Z")

</div>

Closed on 4 December 2020 13:04 UTC

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

[Previous page](https://discourse.nodered.org/t/function-to-go-through-an-array-of-arrays/33567.md?page=1)
