Skip to content

Instantly share code, notes, and snippets.

@rohan-paul
Created September 9, 2016 16:18
Show Gist options
  • Save rohan-paul/830d928481154ac60787b1fb96e12ede to your computer and use it in GitHub Desktop.
Save rohan-paul/830d928481154ac60787b1fb96e12ede to your computer and use it in GitHub Desktop.
To store a grid of values, we have several options. We can use an array of row arrays and use two property accesses to get to a specific square, like this:
var grid = [["top left", "top middle", "top right"], ["bottom left", "bottom middle", "bottom right"]];
console.log(grid[1][2]);
// . bottom right
Or we can use a single array, with size width × height, and decide that
the element at (x,y) is found at position x + (y × width) in the array.
var grid = ["top left", "top middle", "top right", "bottom left", "bottom middle", "bottom right"];
console.log(grid[2 + (1 * 3)]);
// . bottom right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment