6

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've tried a variety of things but they all seem messy. i suppose some things just can't be as clean as Ruby, but how would you approach this?

maybe i'll learn a JS trick or two ;)

EDIT It was revealed that this Ruby code does not actually create 3 arrays. It creates 1 array, that the others reference. This was not the intention. I am looking for a way to easily map a 2 dimensional array with X amount of elements, and Y amount of nested elements in JS.

Also... This is a contrived example. the intension is to be able to substitute 3 with any number. this was just an example using 3.

peter
42.3k7 gold badges67 silver badges110 bronze badges
asked Jul 8, 2012 at 2:17

6 Answers 6

4

You can define it like so:

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

Essentially you're explicitly defining it. However, this array contains references to three different arrays (making a total of 4). To make it behave like ruby, you would 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 to the behavior you see in ruby since each element in the array is a reference to the same array (making a total of just two arrays). You can verify this by doing myArray[0][1] = 2; and then inspecting myArray. You should see the second element in each of the arrays in myArray set to 2.

answered Jul 8, 2012 at 2:29
Sign up to request clarification or add additional context in comments.

Comments

2

If you just want an empty array container, just to keep track of the length, or to assign values later on, you can do this, a bit hacky but should work:

var a = [[,,],[,,],[,,]]
a[1][1] = 'foo'
alert(a[1][1]) //foo
answered Jul 8, 2012 at 2:26

Comments

2

The following is valid 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 that array. If you change array[0][2] you also change array[1][2]. Are you sure this is what you want? The equivalent javascript code would be:

var b = [0, 0, 0];
var a = [b, b, b];
answered Jul 8, 2012 at 2:28

4 Comments

Was about to post this. if compressed [[0,0,0],[0,0,0],[0,0,0]] is around same amount of characters than the ruby version.
Oh i like your edit +1, but you'll have to to set b = "[0, 0, 0]";
John, I'm not sure why you want b to be a string. The OP said nothing about strings. I did just add semicolons though.
ah! i did not realize that catch. that was not my intention.
1

In Ruby you can get the desired effect like this

a = Array.new(3) { Array.new(3, 0) }
p a #[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
a[0][0] = 1
p a #[[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 in a browser

answered Jul 8, 2012 at 15:17

Comments

0

I guess 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);
​
answered Jul 8, 2012 at 2:32

2 Comments

But that creates nested arrays the first element of each being a string.
Agree, you'd have to use evil() or mostly find another solution
0

Another way of doing it is:

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]
]
answered Jul 8, 2012 at 2:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.