title: What is the best way to shift a multidimensional array in Javascript?
body: <p>I'm trying to create a javascript function that shifts an array right x units any up y units. It must keep the array size the same, and it must call unloadChunk for elements that are getting shifted off the multidimensional array. Here is my current implementation:</p>
  
  <pre><code>function shift(x, y) {
      if (x &gt; 0) {
          for (var i = 0; i &lt; chunks.length; i++) {
              for (var j = chunks[i].length - 1; j &gt;= 0; j--) {
                  if(j + x &gt; chunks[i].length - 1 &amp;&amp; chunks[i][j]) {
                    unloadChunk(i, j);
                  } 
                  if (j &lt; x) {
                      chunks[i][j] = null;
                  }
                  else {
                      chunks[i][j] = chunks[i][j - x];
                  }
              }
          }
      }
      else if (x &lt; 0) {
          for (var i = 0; i &lt; chunks.length; i++) {
              for (var j = 0; j &lt; chunks[i].length; j++) {
                  if(j + x  &lt; 0 &amp;&amp; chunks[i][j]) {
                    unloadChunk(i, j);
                  } 
                  if (j - x &gt;= chunks[i].length) {
                      chunks[i][j] = null;
                  }
                  else {
                      chunks[i][j] = chunks[i][j - x];
                  }
              }
          }
      }
      if (y &gt; 0) {
          for (var i = 0; i &lt; chunks.length; i++) {
              if (i + y &gt;= chunks.length) {
                  for (var j = 0; j &lt; chunks.length; j++) {
                    if(i - y &lt; 0 &amp;&amp; chunks[i][j]) {
                      unloadChunk(i, j);
                    }
                    chunks[i][j] = null;
                  }
              }
              else {
                  for (var j = 0; j &lt; chunks.length; j++) {
                      if(i - y &lt; 0 &amp;&amp; chunks[i][j]) {
                        unloadChunk(i, j);
                      }
                      chunks[i][j] = chunks[i + y][j];
                  }
              }
          }
      }
      else if (y &lt; 0) {
          for (var i = chunks.length - 1; i &gt;= 0; i--) {
              if (i + y &lt; 0) {
                  for (var j = 0; j &lt; chunks.length; j++) {
                       if(i - y &gt; chunks.length - 1 &amp;&amp; chunks[i][j]) {
                         unloadChunk(i, j);
                       }
                      chunks[i][j] = null;
                  }
              }
              else {
                  for (var j = 0; j &lt; chunks.length; j++) {
                       if(i - y &gt; chunks.length - 1 &amp;&amp; chunks[i][j]) {
                         unloadChunk(i, j);
                       }
                      chunks[i][j] = chunks[i + y][j];
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>If you're having trouble understanding exactly what I want the shift function to do, take a look at <a href="https://jsfiddle.net/brianhoy/j18w4fdh/2/" rel="nofollow">this fiddle</a> and look at the html output.
  My attempt at creating the shift function works, but it has 10 for loops. My question was, is there a more efficient, less verbose way to do this? </p>