# 2-dimensional arrays

**URL:** <https://discourse.nodered.org/t/2-dimensional-arrays/36973>\
**Category:** General\
**Created:** [3 December 2020 23:02 UTC](https://discourse.nodered.org/t/2-dimensional-arrays/36973 "2020-12-03T23:02:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexander77](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@alexander77](https://discourse.nodered.org/u/alexander77)\
**Post date:** [3 December 2020 23:02 UTC](https://discourse.nodered.org/t/2-dimensional-arrays/36973/1 "2020-12-03T23:02:31Z")

</div>

Hi,  
i try to generate a 2 dimensional arrya (10 rows, 20 columns) and initialize them with data within nested for loops.  
My problem is: i cannot find the correct syntax to get it working. [www.w3schools.com](http://www.w3schools.com) has no example for those arrays. The only thing i found is `var person = [];`

So i tried:

```auto
var state = [[],[]];

for (device = 1;device < 11;device++) {
    for (index = 0; index < 20; index++) {
        state[device][index] = 0;
    }
}
flow.set("state",state);

```

but i get "TypeError: Cannot set property '0' of undefined"

Would be happy if anyone has a good hint for me!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [4 December 2020 00:02 UTC](https://discourse.nodered.org/t/2-dimensional-arrays/36973/2 "2020-12-04T00:02:58Z")

</div>

You need to create an array of arrays... here is a simple flow to demonstrate

```auto
[{"id":"c30c7879.98d578","type":"inject","z":"eeee575.b4b80a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":580,"wires":[["707bb032.ac3e9"]]},{"id":"707bb032.ac3e9","type":"function","z":"eeee575.b4b80a8","name":"","func":"var arr = [];\nfor(var r = 0; r < 10; r++) { // for each of 10 rows\n arr[r] = []; \n for(var c = 0; c < 20; c++) { // create 20 columns\n arr[r][c] = (r+1)*(c+1); // with some value in\n } \n}\nmsg.payload = arr; // return the array\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":580,"wires":[["38f6b117.c94b3e"]]},{"id":"38f6b117.c94b3e","type":"debug","z":"eeee575.b4b80a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":580,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![alexander77](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@alexander77](https://discourse.nodered.org/u/alexander77)\
**Post date:** [4 December 2020 06:48 UTC](https://discourse.nodered.org/t/2-dimensional-arrays/36973/3 "2020-12-04T06:48:25Z")

</div>

Good morning,  
looks quite different to my approach ...  
Of course, this solution works fine.  
Where can i find infos like this? w3school seems not to be the best solution.

Thx and kind regards,  
Alexander

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 December 2020 07:42 UTC](https://discourse.nodered.org/t/2-dimensional-arrays/36973/4 "2020-12-04T07:42:59Z")

</div>

A Google search for "js 2 dimensional array" provides lots of answers. In situations like these, I just search search search, try try try and eventually I find the answer. This also has a positive effect... I learn more.

---

<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:** [18 December 2020 07:43 UTC](https://discourse.nodered.org/t/2-dimensional-arrays/36973/5 "2020-12-18T07:43:06Z")

</div>

Closed on 18 December 2020 07:43 UTC

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