I am working with an indexed JavaScript array that contains exactly 81 elements. These elements will form a 9x9 grid, similar to a Sudoku puzzle. Right now, I am trying to figure out the most efficient way to extract row and column values from this array.
For instance, the function getRow(2)
should return an indexed array of 9 values, representing the second row of the 81-element array. Similarly, the getColumn(2)
function should work in a similar manner for columns.
Currently, my getRow()
function looks like this:
function getRow(rowId){
// Indexes for each row in the 81-element array
var rowIndexes = {
1: [0,1,2,3,4,5,6,7,8],
2: [9,10,11,12,13,14,15,16,17],
3: [18,19,20,21,22,23,24,25,26],
4: [27,28,29,30,31,32,33,34,35],
5: [36,37,38,39,40,41,42,43,44],
6: [45,46,47,48,49,50,51,52,53],
7: [54,55,56,57,58,59,60,61,62],
8: [63,64,65,66,67,68,69,70,71],
9: [72,73,74,75,76,77,78,79,80]
};
// New array to store row values
var rowValues = new Array();
for(var i=0; i < 9; i++){
// Extracting row values from the 81-element array
rowValues.push(GRID_ARRAY[rowIndexes[rowId][i]]);
}
return rowValues;
}
Is there a more dynamic and optimal method to calculate both row and column values based on the specified row/column ID?
Thank you!