Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Last active July 22, 2019 09:32
Show Gist options
  • Save bartwttewaall/efe84da770656e6b7689478be94d95d7 to your computer and use it in GitHub Desktop.
Save bartwttewaall/efe84da770656e6b7689478be94d95d7 to your computer and use it in GitHub Desktop.
Traverse a 2d array in single for-loop
function traverse (values, numCols) {
var numRows = values.length / numCols;
for (var i = 0, length = values.length; i < length; i++) {
var row = (i % length / numCols) << 0; // = Math.floor
var col = i % numCols;
console.log(i, row, col);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment