从二维数组中动态获取列

dynamically get columns from 2D array

我有一个二维行数组,我想通过它得到列 coordinates/information 就像我得到行 (rowArr2D)

所以,在我的专栏 (colArr2D) 中,我只是获取数组中的所有第 4 个位置值,因为我在函数 oRowCount 中传递了

我的目标是分别获取所有列。 示例:

Row:[ [ 0, 1, 2, 3, 4, 5, 6 ], [ 0, 1, 2, 3, 4, 5, 6 ], [ 0, 1, 2, 3, 4, 5, 6 ], [ 0, 1, 2, 3, 4, 5, 6 ] ]

Columns: [[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]]

mockTable = { // mocking the portions of my code
  GetRowsCount : () => 4,
  GetRow: (x) => ({
    GetCellsCount : () => 7,
    GetCell : (x) => x
  })
}

CTable_prototype_GetTableMapping = function(currentTable)
{
        //get row information
    let oRowCount = currentTable.GetRowsCount();
    const rowArr2D = Array(oRowCount);
    for (let i = 0; i < oRowCount; i++) {
        //get cell information and cell count
        let oRow = currentTable.GetRow(i);
        let oCellCount = oRow.GetCellsCount();
        rowArr2D[i] = Array(oCellCount);
        for (let j = 0; j < oCellCount; j++) {
            //get cell content 
            let oCell = oRow.GetCell(j);
            rowArr2D[i][j] = oCell;
        }
    }
    // get column information
    const colArr2D = (array, colCount) => {
        const result = [];

        array.forEach(e => {
            result.push(e[colCount]);
        });
        console.log(result);
        return result;
    };
    colArr2D(rowArr2D, oRowCount);
  return rowArr2D
    console.log(rowArr2D);
};

const theArray = CTable_prototype_GetTableMapping(mockTable);

console.log("full 2D array", theArray)

试试这个

const colArr2D = (array) => 
  array[0].map((a, i) => 
    array.map(b => b[i])
  );

const arr = [[1,2,3],[4,5,6],[7,8,9]];

console.log(colArr2D(arr))