# For loop iteration for arrays

**URL:** <https://discourse.nodered.org/t/for-loop-iteration-for-arrays/52436>\
**Category:** General\
**Created:** [19 October 2021 18:29 UTC](https://discourse.nodered.org/t/for-loop-iteration-for-arrays/52436 "2021-10-19T18:29:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecymb1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ecymb1/32/49640_2.png) [@ecymb1](https://discourse.nodered.org/u/ecymb1)\
**Post date:** [19 October 2021 18:29 UTC](https://discourse.nodered.org/t/for-loop-iteration-for-arrays/52436/1 "2021-10-19T18:29:12Z")

</div>

I am trying to **browse** through an **array of arrays** through a **FOR loop** , but each time it gives me an unjustified error. When I _remove the loop, the code works perfectly_. The **large array** basically has these **small arrays** that have **only one element** ; when I iterate through them, i get the error. Any help would be greatly appreciated.

The **error** : "TypeError: Cannot read property '0' of undefined"

This is the problematic section of the **code** :

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

Here's the **array** (from the spreadsheet file):

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

Heads up, you will need **node-red-contrib-spreadsheet-In** for the following flow.  
Here's my **flow** : `[{"id":"ecfc5735.e93868","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"b17374d1.6f27a8","type":"inject","z":"ecfc5735.e93868","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"selectAddress","v":"A3","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":230,"y":160,"wires":[["95e6fb19.6c6d48"]]},{"id":"9a18f2e3.8bc87","type":"file in","z":"ecfc5735.e93868","name":"Load","filename":"C:\\Users\\a0540\\Downloads\\Locations.xlsx","format":"","chunk":false,"sendError":false,"x":590,"y":160,"wires":[["6c5a988e.f5bbb8"]]},{"id":"6c5a988e.f5bbb8","type":"book","z":"ecfc5735.e93868","name":"","raw":false,"x":590,"y":200,"wires":[["f13f2c0f.093b8"]]},{"id":"f13f2c0f.093b8","type":"sheet","z":"ecfc5735.e93868","name":"","sheetName":"Sheet1","x":590,"y":240,"wires":[["a48eaa74.321248","31960c34.0193a4"]]},{"id":"a48eaa74.321248","type":"cell","z":"ecfc5735.e93868","name":"","address":"","dataType":"w","x":590,"y":280,"wires":[["bde977d0.f3cd38"]]},{"id":"bde977d0.f3cd38","type":"debug","z":"ecfc5735.e93868","name":"Debug","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":280,"wires":[]},{"id":"95e6fb19.6c6d48","type":"trigger","z":"ecfc5735.e93868","name":"Hourly Update","op1":"","op2":"0","op1type":"date","op2type":"str","duration":"-1","extend":false,"overrideDelay":false,"units":"hr","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":380,"y":160,"wires":[["9a18f2e3.8bc87"]]},{"id":"31960c34.0193a4","type":"sheet-to-json","z":"ecfc5735.e93868","name":"","raw":"false","range":"A2:A366","header":"1","blankrows":false,"x":610,"y":320,"wires":[["76e54a21.c26c64"]]},{"id":"57efdde7.e70214","type":"debug","z":"ecfc5735.e93868","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":890,"y":320,"wires":[]},{"id":"76e54a21.c26c64","type":"function","z":"ecfc5735.e93868","name":"","func":"/*moment = global.get('moment');\ntoday = moment(Date()).format('M/D/YY');\n\nfor (let i = 0; i < 365; i++) \n{\n if (today === msg.payload[55][0])\n {\n msg.payload = i;\n break;\n }\n else\n {msg.payload = msg.payload[1][0];}\n}\n*/\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":320,"wires":[["57efdde7.e70214"]]}]This text will be hidden`

---

<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:** [19 October 2021 19:19 UTC](https://discourse.nodered.org/t/for-loop-iteration-for-arrays/52436/2 "2021-10-19T19:19:57Z")

</div>

The first time your loop does not equal `today` your else statement overwrites msg.payload, so the next iteration msg.payload is no longer an array.

---

<div class="post-metadata">

**Author:** ![ecymb1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ecymb1/32/49640_2.png) [@ecymb1](https://discourse.nodered.org/u/ecymb1)\
**Post date:** [19 October 2021 19:47 UTC](https://discourse.nodered.org/t/for-loop-iteration-for-arrays/52436/3 "2021-10-19T19:47:36Z")

</div>

It worked! Thank you so much for dedicating your precious time to this. Much appreciate it.

---

<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:** [2 November 2021 19:48 UTC](https://discourse.nodered.org/t/for-loop-iteration-for-arrays/52436/4 "2021-11-02T19:48:09Z")

</div>

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