如何使一个二维数组迭代?



我有一个表,通过它我想获得行和列坐标,如表中有Row-2和col-2

坐标Row = [0,1]Col = [0,1,0,1].

由于我将其存储在数组中,我想要一种更好的方法将其存储在二维数组中,以便我可以遍历它。考虑到如果表有超过7行和cols是更好的有一个二维数组?

方法我写的存储在数组中,我如何在其中创建一个二维数组?

CTable.prototype.GetTableMapping = function(currentTable)
{
let oRowCount = currentTable.GetRowsCount();
let oRowMapping = [];
let oColumnMapping = [];
let oTableMapping = [oRowMapping = [], oColumnMapping = []];
for (let i = 0; i < oRowCount; i++)
{
let oRow = currentTable.GetRow(i);
let oCellCount = oRow.GetCellsCount();
oRowMapping.push(i);
for (let j = 0; j < oCellCount; j++)
{
let oCell = oRow.GetCell(j);
oColumnMapping.push(j);
}
}
console.log("Table",oTableMapping);
console.log("Rows",oRowMapping);
console.log("Columns",oColumnMapping);
return oTableMapping[oRowMapping,oColumnMapping];
};
Output:
[
Row = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
Cols = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
]

既然你已经有了双for循环你可以用它来创建2D单元格

arr2D[i][j] = oCell;

完整的示例(mock):

mockTable = { // mocking the portions of your code that i don't know
GetRowsCount : () => 11,
GetRow: (x) => ({
GetCellsCount : () => 4,
GetCell : (x) => x
})
}
CTable_prototype_GetTableMapping = function(currentTable)
{
let oRowCount = currentTable.GetRowsCount();
const arr2D = Array(oRowCount);
//let oRowMapping = [];
//let oColumnMapping = [];
//let oTableMapping = [oRowMapping = [], oColumnMapping = []];
for (let i = 0; i < oRowCount; i++)
{
let oRow = currentTable.GetRow(i);
let oCellCount = oRow.GetCellsCount();
arr2D[i] = Array(oCellCount);
//oRowMapping.push(i);
for (let j = 0; j < oCellCount; j++)
{
let oCell = oRow.GetCell(j);
//oColumnMapping.push(j);
arr2D[i][j] = oCell;
}
}
//console.log("Table",oTableMapping);
//console.log("Rows",oRowMapping);
//console.log("Columns",oColumnMapping);
return arr2D;
};
const theArray = CTable_prototype_GetTableMapping(mockTable);
console.log("cell (1,3)",theArray[1][3])
console.log("full 2D array", theArray)

最新更新