Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mirror a Multidimmensional array in javascript?

I need to be able to mirror a multidimensional array that changes in size. I'm currently hard coding for each particular array size and it is horribly inefficient.

Example:

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

Mirrored:

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

Arrays range in size from 2x5 to 4x10.

like image 452
J. Celestino Avatar asked Nov 23 '11 18:11

J. Celestino


Video Answer


1 Answers

Ok, so all you need is a horizontal mirror. I suppose that your array contains an array for every line, so that means that you just need to reverse every row.

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

or even better

multiarr.map(function(arr){return arr.reverse();});
like image 180
bigblind Avatar answered Sep 19 '22 12:09

bigblind