title: How to check values of adjacent items in a list? Javascript
body: <p>I have a list of list that looks something like this</p>
  <pre><code>let pixels = [
   [0,0,0,0]
   [0,0,0,0]
   [0,0,0,0]
  ]
  </code></pre>
  <p>and I would like to iterate through this list and create a list of the adjacent items for each item. In this example the output I would want from pixels[0][0] is: [undefined, undefined, undefined, undefined, 0, undefined, 0, 0]. But when I run my code I get I get an error</p>
  <p>The code cannot read pixels[-1][-1], I just want it to read it as undefined and not crash the whole program.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let pixels = [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]
  ]
  
  function adjacent(i1, i2) {
    const yep = [pixels[i1 - 1][i2 - 1], pixels[i1 - 1][i2], pixels[i1 - 1][i2 + 1], pixels[i1][i2 - 1], pixels[i1][i2 + 1], pixels[i1 + 1][i2 - 1], pixels[i1 + 1][i2], pixels[i1 + 1][i2 + 1]]
    return yep
  }
  
  console.log(adjacent(1,4))
  
  console.log(adjacent(-1,-1))</code></pre>
  </div>
  </div>
  </p>