Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

matrix transpose javascript

//One-liners inspired by Fawad Ghafoor and Óscar Gómez Alcañiz

function transpose(matrix) {
  return matrix[0].map((col, i) => matrix.map(row => row[i]));
}

function transpose(matrix) {
  return matrix[0].map((col, c) => matrix.map((row, r) => matrix[r][c]));
}
//Functional approach style with reduce by Andrew Tatomyr

function transpose(matrix) {
  return matrix.reduce((prev, next) => next.map((item, i) =>
    (prev[i] || []).concat(next[i])
  ), []);
}
//Lodash/Underscore by marcel

function tranpose(matrix) {
  return _.zip(...matrix);
}

// Without spread operator.
function transpose(matrix) {
  return _.zip.apply(_, [[1,2,3], [1,2,3], [1,2,3]])
}
Even simpler Lodash/Underscore solution by Vigrant

_.unzip(matrix);
Vanilla approach

function transpose(matrix) {
  const rows = matrix.length, cols = matrix[0].length;
  const grid = [];
  for (let j = 0; j < cols; j++) {
    grid[j] = Array(rows);
  }
  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
      grid[j][i] = matrix[i][j];
    }
  }
  return grid;
}
//Vanilla in-place ES6 approach inspired by Emanuel Saringan

function transpose(matrix) {
  for (var i = 0; i < matrix.length; i++) {
    for (var j = 0; j < i; j++) {
      const temp = matrix[i][j];
      matrix[i][j] = matrix[j][i];
      matrix[j][i] = temp;
    }
  }
}

// Using destructing
function transpose(matrix) {
  for (var i = 0; i < matrix.length; i++) {
    for (var j = 0; j < i; j++) {
      [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
    }
  }
}
Comment

transpose of the matrix in javascript

class Solution {
    solve(matrix) {
        let arr=[];
        for(let i=0;i<matrix.length;i++){
            arr.push([])
            for(let j=0;j<matrix.length;j++){
                arr[i].push(matrix[j][i])
            }
        }
        return arr
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript replace hyphen with space 
Javascript :: javascript interval fixed number of times 
Javascript :: jspdf save in server 
Javascript :: add value to each object in array javascript 
Javascript :: input clear 
Javascript :: javascript Inserting values in between an array 
Javascript :: datatables ajax.reload(); 
Javascript :: canvas js filter greyscale 
Javascript :: javascript cors error 
Javascript :: select text with javascript 
Javascript :: js select div 
Javascript :: get value of radio button javascript 
Javascript :: js add html element to div 
Javascript :: remove line break javascript 
Javascript :: how to make form in javascript 
Javascript :: slide out navigation 
Javascript :: routes react 
Javascript :: jquery upload image 
Javascript :: On pressing enter change the focus to the next input field 
Javascript :: iterate through array js 
Javascript :: set a value in session using javascript 
Javascript :: countdown in react js 
Javascript :: not in array js 
Javascript :: javascript new date dd/mm/yyyy 
Javascript :: javascript Using debugger 
Javascript :: How to make blinking/flashing text with jQuery 
Javascript :: react native navigation navigate 
Javascript :: es6 iife 
Javascript :: json limit 
Javascript :: jquery change title of page 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =