Forum Moderators: open

Message Too Old, No Replies

two-dimensional Array initialization

         

bzgzd

10:25 am on Jan 6, 2011 (gmt 0)

10+ Year Member



I would like to create in JavaScript an Array of integers representing a grid.

For example something like 20x15 (those two dimensions are in variables) and I would like to "fill" all values with number 1 at the beginning.

Is there a way how to do it directly by declaration or I need to go through all elements with two for loops?

This is what I found in some tutorial:
var g_puzzleArray = new Array(g_rows);
for (ii = 0; ii < g_rows; ii++)
{
g_puzzleArray[ii] = new Array(g_columns);
for (j = 0; j < g_columns; j++)
{
g_puzzleArray[ii][j] = 1;
}
}
Is it really the best way how to do it? (i would expect some one liner...)

Fotiman

2:59 pm on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is no way to initialize all of the values in the array without either looping or defining the array as a literal. For example:

var g_puzzleArray = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];


But I don't think that's what you're going for.

As a side note, you don't need to do "new Array(g_rows)" and "new Array(g_columns)". You can avoid the overhead of calling "new" and use empty array literals instead:

var g_puzzleArray = [];
for (ii = 0; ii < g_rows; ii++) {
g_puzzleArray[ii] = [];
for (j = 0; j < g_columns; j++) {
g_puzzleArray[ii][j] = 1;
}
}


Or, for a one liner approach:


var g_puzzleArray = []; for (ii = 0; ii < g_rows; ii++) { g_puzzleArray[ii] = []; for (j = 0; j < g_columns; j++) { g_puzzleArray[ii][j] = 1; } }

;)
I was, of course, only kidding with that one liner approach... it's just the previous code with newlines removed.

bzgzd

10:15 pm on Jan 6, 2011 (gmt 0)

10+ Year Member



Without using "new" keyword, is it possible to provide also information about number of array entries?

Or is that parameter not important or ignored?

"new Array(g_rows)" is overhead because of more letters in JavaScript code or is it doing something different then just "[]" ?

Fotiman

2:02 pm on Jan 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Without using the new keyword, no. But since you're then creating all of the entries by looping, it's not really needed. And since you could still add more to the array than it is initially sized to, it doesn't really give you anything.

When you're calling "new", you're going to be creating a new object by calling it's constructor function, so there is slightly more work that will happen. With [], you're simply declaring a literal array object, so slightly less overhead. And yes, it is less letters, so you're saving a few bytes there as well. In most cases you probably won't notice any speed difference, but there is a slight performance improvement using [] instead of new Array().

daveVk

7:35 am on Jan 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



new Array(g_rows) will create an array with one entry r_row, I don't think that is what you intended, its the same as [ g_rows ].

bzgzd

11:50 am on Jan 8, 2011 (gmt 0)

10+ Year Member



In one tutorial I saw that reason to better not use "new" keyword for creating arrays in JS is that people could be confused... [hunlock.com ]

new Array(g_rows) will create an array with one entry
This would be true only if g_rows would be not number.

After this code:
var g_test = 10;
var test = new Array(g_test);
alert(test[0]);
alert(test.length);
Alerts are "undefined" and 10

daveVk

1:24 pm on Jan 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This would be true only if g_rows would be not number.


I was not aware of that, irregularities like that are a real trap.

q1 = new Array( 3, 5 );
q2 = new Array( 4 );
...

good reason to use [ ] syntax.