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 :: remove spaces in a string js 
Javascript :: window.location.href jquery 
Javascript :: jquery check how many child elements 
Javascript :: how to check variable type jquery 
Javascript :: javascript change url without redirect 
Javascript :: scroll to element jquery 
Javascript :: js loop through array backwards 
Javascript :: javascript async delay 
Javascript :: javascript clear localstorage 
Javascript :: generate random whole numbers within a range javascript 
Javascript :: function time javascript 
Javascript :: js window resize listener 
Javascript :: JS Fetch API Post Request 
Javascript :: jquery pause n seconds 
Javascript :: bootstrap modal title center 
Javascript :: br in react native 
Javascript :: Drupal 8 get page node current path 
Javascript :: jquery check if empty object 
Javascript :: disable eslint for line 
Javascript :: call a function on load jquery 
Javascript :: urlencode javascript 
Javascript :: set value in span using javascript 
Javascript :: javascript start function on load 
Javascript :: react native run ipad 
Javascript :: yup email validation 
Javascript :: Remove specific object from the Array in Javascript 
Javascript :: how to set an attribute to ignore null value json c# 
Javascript :: just number regex js 
Javascript :: godot destroy node 
Javascript :: manage favicon with express app 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =