0

Sorry if this is too basic, but I am struggling at defining 4-dimensional array (of size 6x6x6x6) in JavaScript and initializing it to all 1's. What's the easiest way to do this?

Thanks!

8
  • 1
    possible duplicate of JavaScript Multidimensional Arrays Commented Jun 7, 2012 at 23:45
  • Javascript does not have multidimensional arrays but rather arrays of arrays. Commented Jun 7, 2012 at 23:45
  • @Frankie multidimensional arrays are just arrays of arrays. Commented Jun 7, 2012 at 23:48
  • What do you mean by of size 6,6,6,6? Commented Jun 7, 2012 at 23:50
  • @ddlshack: 4-dimensional array with size of each dimension = 6, obviously. [[[[1]]]] <--- this is example of 1x1x1x1 Commented Jun 7, 2012 at 23:52

3 Answers 3

3

You can use the literal syntax, but it would be very big and cumbersome. You may want to try something like this:

var x = [1, 1, 1, 1, 1, 1]; for (var i = 1; i < 4; i++) { x = [x, x, x, x, x, x]; } 

I found a slightly simpler solution:

var x = 1; for (var i = 0; i < 4; i++) { x = [x, x, x, x, x, x]; } 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! btw, this question wasn't a challenge, i needed this for a combinatorics-related problem and you saved my day :) funny that defining an array could be so unintuitive...
1

Seems like there should be easier way, but this will do it.

var array = []; for(var i=0; i<6; i++) { for(var j=0; j<6; j++) { for(var k=0; k<6; k++) { for(var l=0; l<6; l++) { array[i][j][k][l]=1; } } } } 

Edit

To generate an n-dimensional AxBxCxDx... array (untested):

Array.prototype.fill = function(elem, n) { for(var i=0; i<n; i++, this.push(elem)); } function generateArray() { var dimensions = Array.prototype.slice.call(arguments); var x = 1; for (var i = dimensions.length-1; i >= 0; i--) { x = [].fill(x, dimensions[i]); } return x; } 

to generate a 2x3x4x5 matrix:

generateArray(2,3,4,5); 

1 Comment

mikez302's solution is better. However, this will allow you to create an AxBxCxD matrix.
0

I implemented ddlshack's generalized method, but ran into an issue due to the fact that arrays are "pass by reference" in JavaScript. This resulted in each dimension of the array holding multiple references to the same array rather than copies of it. To correct the issue, I implemented the solution as follows (the only other difference being that I used a second function rather than modify Array's prototype).

var fillArray = function(val, dim) { var a = []; for (var i = 0; i < dim; i++) { if (Object.prototype.toString.call(val) === "[object Array]") { val = val.slice(0); } a.push(val); } return a; }; var generateArray = function() { var dimensions = Array.prototype.slice.call(arguments), val = 0; for (var i = (dimensions.length - 1); i >= 0; i--) { val = fillArray(val, dimensions[i]); } return val; }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.