For loop iteration for arrays

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:

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

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

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.

1 Like

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

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