# "Rotate" 2D array

**URL:** <https://discourse.nodered.org/t/rotate-2d-array/59761>\
**Category:** General\
**Created:** [12 March 2022 17:25 UTC](https://discourse.nodered.org/t/rotate-2d-array/59761 "2022-03-12T17:25:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geppy57](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Geppy57](https://discourse.nodered.org/u/Geppy57)\
**Post date:** [12 March 2022 17:25 UTC](https://discourse.nodered.org/t/rotate-2d-array/59761/1 "2022-03-12T17:25:57Z")

</div>

Hi all.  
I'm testing a system with MLX90640 and the image is satisfying.  
In order to get a valid application for my specific case, I need to rotate 90 degrees the original matrix to get landscape orientation. (for data, not only for image).  
With node-red-contrib-ui-heatmap I see the target with correct 24 columns and 32 rows.  
My application need to divide the columns in three groups with 8 columns each in order to verify the maximum temperature in each zone.  
Actually I have convert the original 32 x 24 array in a 24 x 32 array but I need to transfer data from columns in the first array to rows in the second.  
I found an example (see attached file) where it's possible to extract a single column.  
Is it possible to indexing the pointer in order to extract all columns?

BR

[Extract column from 2D array.json](https://discourse.nodered.org/uploads/short-url/uNEWA0tc6iwW63q1tGiPOQ0DEWz.json) (2.4 KB)

---

<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:** [12 March 2022 18:08 UTC](https://discourse.nodered.org/t/rotate-2d-array/59761/2 "2022-03-12T18:08:11Z")

</div>

Sure apply the map to each index position in a for loop.

```auto
let output = []
let depth = msg.payload[0].length
// take every column
for(let i = 0; i < depth; i++){
    let col = msg.payload.map((value) => value[i]);
    output.push(col);
}
msg.payload= output;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Geppy57](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Geppy57](https://discourse.nodered.org/u/Geppy57)\
**Post date:** [13 March 2022 09:38 UTC](https://discourse.nodered.org/t/rotate-2d-array/59761/3 "2022-03-13T09:38:54Z")

</div>

Hi.  
First of all, thanks for your answer.  
I try to put the lines inside a function block, but the attached warning appears.  
If I launch the program, the output is an empty array.  
Something else to do / test to solve the question?  
BR

 ![Warning_JSON](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f2560252e288794a985d9681f117a3fdc675980.jpeg)

---

<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:** [13 March 2022 09:47 UTC](https://discourse.nodered.org/t/rotate-2d-array/59761/4 "2022-03-13T09:47:30Z")

</div>

The warning is just a warning telling you that a function(the map) inside a loop referring to outside vars "may" cause an issue. In this case it will not, so can be ignored.

As to your result, seems strange as when i drop the function in your flow I get a result

```auto
[{"id":"e061ca3d.2e315","type":"inject","z":"bf9e1e33.030598","name":"Go","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"1","payloadType":"str","x":190,"y":2100,"wires":[["203e0898.9e7e2"]]},{"id":"203e0898.9e7e2","type":"function","z":"bf9e1e33.030598","name":"fill numeric array (3 x 4)","func":"//var arr =[25,19,24,18,32,31,24,12,29,22,12,14]\nvar arr = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]];\nmsg.payload = arr;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":2180,"wires":[["b6b6fec1.94c588","b8a4ae21.613328"]]},{"id":"b6b6fec1.94c588","type":"debug","z":"bf9e1e33.030598","name":"Array raw","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":2140,"wires":[]},{"id":"b8a4ae21.613328","type":"function","z":"bf9e1e33.030598","name":"","func":"let output = []\nlet depth = msg.payload[0].length\n// take the third column\nfor(let i = 0; i < depth; i++){\n let col = msg.payload.map((value) => value[i]);\n output.push(col);\n}\nmsg.payload= output;\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":2180,"wires":[["f46fae95.e94f9"]]},{"id":"f46fae95.e94f9","type":"debug","z":"bf9e1e33.030598","name":"desired column","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":2180,"wires":[]}]

```

output

```auto
[[1,4,7,10],[2,5,8,11],[3,6,9,12]]

```

So it must be something you have added or removed, hard to tell any thing from the image you supplied.

---

<div class="post-metadata">

**Author:** ![Geppy57](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Geppy57](https://discourse.nodered.org/u/Geppy57)\
**Post date:** [13 March 2022 09:52 UTC](https://discourse.nodered.org/t/rotate-2d-array/59761/5 "2022-03-13T09:52:52Z")

</div>

Sorry.  
It's my fault.  
I put your sample into a different flow without formatting the input array.  
Sorry for the inconvenience.  
Thank you.

---

<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:** [27 March 2022 09:53 UTC](https://discourse.nodered.org/t/rotate-2d-array/59761/6 "2022-03-27T09:53:53Z")

</div>

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