Ruby => Javascript translation

curious ... how would you write this Ruby in JS?

Array.new(3, Array.new(3, 0)) 

which returns

 [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 

I tried different things, but they all seem messy. I suppose some things just might not be as clean as Ruby, but how would you approach this?

Maybe I recognize a JS trick or two;)

EDIT It turned out that this Ruby code does not actually create 3 arrays. It creates 1 array that is referenced by the rest. That was not the goal. I am looking for a way to easily map a 2-dimensional array with X number of elements and Y number of nested elements in JS.

Also ... This is a contrived example. the intention is to be able to replace 3 with any number. it was just an example using.

+6
source share
6 answers

You can define it like this:

 var arr = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; 

Essentially, you explicitly define it. However, this array contains references to three different arrays (4 in total). To make it behave like a ruby, you need to create a function that mimics this behavior:

 function arr(size, element) { var ret = []; for(var i = 0; i < size; i++) { ret.push(element); } return ret; } 

Then you could do:

 var myArray = arr(3, arr(3, 0)); //myArray contains [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 

This is more true for the behavior that you see in ruby, since each element of the array is a reference to the same array (a total of only two arrays). You can verify this by running myArray[0][1] = 2; and then checking myArray . You should see the second element in each of the arrays in myArray set to 2.

+4
source

If you just need an empty array container, just to track the length or assign values ​​later, you can make this a bit hacky, but should work:

 var a = [[,,],[,,],[,,]] a[1][1] = 'foo' alert(a[1][1]) //foo 
+2
source

The following is the Javascript syntax, assuming you want to create 4 arrays:

 [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 

EDIT:

Actually your ruby ​​code only creates two arrays. One array is [0,0,0] and the other array contains three references to this array. If you change array[0][2] , you will also change array[1][2] . Are you sure this is what you want? The equivalent javascript code would look like this:

 var b = [0, 0, 0]; var a = [b, b, b]; 
+2
source

In Ruby, you can get the desired effect:

 a = Array.new(3) { Array.new(3, 0) } pa #[[0, 0, 0], [0, 0, 0], [0, 0, 0]] a[0][0] = 1 pa #[[1, 0, 0], [0, 0, 0], [0, 0, 0]] 

In js like this

 var multi_dim = function(a, b, value) { var myObj = []; for (i=0;i<a;i++) { myObj[i] = []; for (j=0;j<b;j++) { myObj[i][j] = value; } } return myObj; }; var c = multi_dim(3,3,0); WScript.echo(c); //0,0,0,0,0,0,0,0,0 c[0][0]=1 WScript.echo(c); //1,0,0,0,0,0,0,0,0 

replace WScript.echo with document.write if not in windows and / or browser

+1
source

I think you could just do this: http://jsfiddle.net/PZBUr/

 var i = "[0, 0, 0]"; var myArray = new Array(i, new Array(i, new Array(i))); document.write(myArray);​ 
0
source

Another way to do this:

 var arr = new Array( new Array(0, 0, 0), new Array(0, 0, 0), new Array(0, 0, 0) ); 

or simply

 var arr = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ] 
0
source

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


All Articles