Skip to content

Instantly share code, notes, and snippets.

@amimaro
Last active March 22, 2018 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amimaro/e8d01cc5330d1f9f28eb87d0218ce84d to your computer and use it in GitHub Desktop.
Save amimaro/e8d01cc5330d1f9f28eb87d0218ce84d to your computer and use it in GitHub Desktop.
Convert Array Index into Matrix Index
let matrix = Array.from(Array(100).keys());
let rows = 10, columns = 10;
let getRow = (index, totalRows) => {
return Math.floor(index/totalRows);
}
let getColumn = (index, totalColumns) => {
return index % totalColumns;
}
matrix.map((val, index) => {
console.log('Index: ' + index, 'Row: ' + getRow(index, rows), 'Column: ' + getColumn(index, columns));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment