How can I mirror a multidimensional array in javascript?

I need to be able to mirror a multidimensional array that changes in size. I am currently hard coding for each specific size of the array, and it is terribly inefficient.

Example:

Arr1 { 1 2 3 Arr2 { 1 2 4 5 6 3 4 7 8 9 } 5 6 } 

Markup:

  { 3 2 1 { 2 1 6 5 4 4 3 9 8 7 } 6 5 } 

Arrays have sizes from 2x5 to 4x10.

+4
source share
2 answers

So, all you need is a horizontal mirror. I suppose your array contains an array for each row, so that means that you just need to undo all rows.

 for(var i=0;i<multiarr.length;i++){ multiarr[i].reverse(); } 

or even better

 multiarr.map(function(arr){return arr.reverse();}); 
+3
source
 For each of the lines: For i = 0 to width/2: arr[line][i] <-> arr[line][width - i] 

Doesn't that work?

0
source

Source: https://habr.com/ru/post/1382752/


All Articles