我有一個表格,我想通過它獲取行和列坐標,就像它有 Row-2 和 col-2
坐標
Row = [0,1]和Col = [0,1,0,1]。
由于我將它存盤在一個陣列中,我想要一種更好的方法將它存盤在二維陣列中,以便我可以對其進行迭代。考慮到表是否有超過 7 行和列,擁有一個二維陣列是否更好?
我寫的方法將它存盤在陣列中,我如何在其中制作一個二維陣列?
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]
]
uj5u.com熱心網友回復:
由于您已經有一個雙 for 回圈,您可以使用它來創建 2D 單元格
arr2D[i][j] = oCell;
完整示例(模擬):
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/461270.html
標籤:javascript 数组 for循环
上一篇:在for回圈中使用str.replace而不會丟失最后一次迭代
下一篇:設定被讀取為變數的當前檔案的名稱
