Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Arrays in JavaScript have quirks of their own that you may not be expecting if you come from other languages. Two important ones for your use case are:</p> <ol> <li>You cannot directly declare multidimension arrays in JavaScript.</li> <li>There's little efficiency benefit (and no added safety) when you set the size of the array at creation. </li> </ol> <p>Unlike other languages, JavaScript won't allocate a block of memory for the full array. (It doesn't know what kind of objects you're going to be putting in each cell, and therefore how much total memory it will need.) Instead, all the <code>size</code> argument to <code>Array()</code> does for you is set the array's <code>length</code> property.</p> <p>For the general, 2d array case, I'd suggest:</p> <ol> <li><p>Create the "top" array, e.g.:</p> <pre><code>var i // the first-order index in a , j // the second order index in a , a = [] </code></pre></li> <li><p>Initialize array elements as needed. This is called <a href="http://en.wikipedia.org/wiki/Lazy_initialization">lazy initialization</a>, and, in this case, it simply involves testing that <code>a[i]</code> exists before we try to assign something to <code>a[i][j]</code>, e.g.:</p> <pre><code>if (!a[i]) a[i] = [] </code></pre> <p>In English the above statement reads: "If the i-th element of <code>a</code> is 'falsy', assign an empty array to the i-th element."</p></li> <li><p>Finally, assign the actual value to the multideminsional array:</p> <pre><code>a[i][j] = 'whatever' </code></pre></li> </ol> <p>For your case, you know the values ahead of time, so you can initialize each element in advance. (If you're not overriding most of the elements, however, a lazy implementation may be better; see below.)</p> <pre><code>var x, x_length = 100 , y, y_length = 100 , map = [] // Don't be lazy for (x = 0; x &lt; x_length; x++) { map[x] = [] for (y = 0; y &lt; y_length; y++) { map[x][y] = 'grass.gif|ongrass.gif|collision.gif|above.gif' } } </code></pre> <p>As some others have said, an array with 100 elements has indexes numbered from <em>zero</em> to <em>ninety-nine</em>, so a less-than comparison is most appropriate here.</p> <hr> <p>For reference, here's an implementation that uses lazy initialization. I've gone with a function interface instead of directly accessing the array; it's longer and more complex, but also more complete.</p> <p>The initialization pattern I've used here is called an <a href="http://benalman.com/news/2010/11/immediately-invoked-function-expression/">immediately invoked function expression</a>. If you haven't seen it before, it's one of the more useful JavaScript patterns and well worth taking some time to understand.</p> <pre><code>var map = (function (x_length, y_length, v_default, undefined) { // Unless v_default is overwritten, use ... v_default = v_default || 'grass.gif|ongrass.gif|collision.gif|above.gif' // Private backing array; will contain only values for a[x][y] // that were explicitly set. var a = [] // Private helper function. // - Returns `true` if `x` is between `0` and `x_length - 1` // and `y` is between `0` and `y_length - 1`. // - Returns `false` otherwise. function valid (x, y) { return (x &gt;= 0 &amp;&amp; x &lt; x_length &amp;&amp; y &gt;= 0 &amp;&amp; y &lt; y_length) } // Private helper function. // - Returns `true` if a[x][y] has been set(). // - Returns `false` otherwise. function exists (x, y) { return !!a[x] &amp;&amp; !!a[x][y] } // Private getter // - Returns the value of a[x][y] if it has been set(). // - Returns `undefined` if the point (x,y) is invalid. // - Returns `v_default` otherwise. function get (x, y) { if (!valid(x, y)) return undefined else if (exists(x, y)) return a[x][y] else return v_default } // Private setter // - Returns the value set on success. // - Returns `undefined` on failure function set (x, y, v) { if (valid(x, y)) { // We're being lazy if (!a[x]) a[x] = [] a[x][y] = v return a[x][y] } return undefined } // Return an interface function. // - Pass the function three arguments, (x, y, v), to set a[x][y] = v // - Pass the function two arguments, (x, y), to get a[x][y] return function (x, y, v) { if (arguments.length &gt; 2) { return set(x, y, v) } else { return get(x, y) } } })(100, 100) </code></pre> <p>When I ran the above in node, the following tests printed sensible values:</p> <pre><code>// Invalid invocations console.log('map() : %s', map()) console.log('map( 0) : %s', map(0)) console.log('map( -1, 0) : %s', map(-1,0)) console.log('map( 0, -1) : %s', map(0, -1)) console.log('map( -1, -1) : %s', map(-1, -1)) // Valid invocations console.log('map( 0, 0) : %s', map(0, 0)) console.log('map( 99, 99) : %s', map(99, 99)) console.log('map( 1, 1) : %s', map(1,1)) console.log('map( 1, 1, "foo") : %s', map(1,1, 'foo')) console.log('map( 1, 1) : %s', map(1,1)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload