Home > database >  dynamically get columns from 2D array
dynamically get columns from 2D array

Time:04-20

I have a 2D array of row,through which i want get the column coordinates/information just like i got for the row(rowArr2D)

So,in my Column(colArr2D) i'm just getting all 4th position values in the array since i passed have oRowCount in the function

my goal is to get all columns respectively. Example:

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 your code that i don't know
  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)

CodePudding user response:

Give this a try

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))

  • Related