Javascript Array transpose()

Description

Javascript Array transpose()


Array.prototype.transpose = function () {
  var transposed = [[],[],[]];

  for( var i = 0; i < this.length; i++) {
    for( var j = 0; j < this[i].length; j++) {
      var new_val = this[j][i]
      transposed[i][j] = new_val;/*from  w  ww .j  a  va  2s. c o  m*/
    };
  };

  return transposed;
};

a = [[0,1,2],[3,4,5],[6,7,8]]

console.log(a.transpose());

Javascript Array transpose()

Array.prototype.transpose = function() {
  var newArr = []
  for (var i = 0; i <= 2; i++) {
    newArr.push([]);//from  w w w .  ja v  a2  s .c  o  m
    for (var j = 0; j <= 2; j ++) {
      var originalElement = this[i][j];
      newArr[i].push(this[j][i]);
    }
  }
  return newArr;
};

console.log([[1, 2, 3], [4, 5, 6], [7, 8, 9]].transpose())

Javascript Array transpose()

Array.prototype.transpose = function() {
  var a = this, w = a.length ? a.length : 0, h = a[0] instanceof Array ? a[0].length
      : 0;//from  w w  w. ja  va  2 s .com
  if (h === 0 || w === 0) {
    return []
  }
  var i, j, t = [];
  for (i = 0; i < h; i++) {
    t[i] = [];
    for (j = 0; j < w; j++) {
      t[i][j] = a[j][i]
    }
  }
  return t
};
if (typeof jQuery !== 'undefined') {
  jQuery.transpose = function(o) {
    if (o instanceof Array) {
      return o.transpose()
    }
  }
}

Javascript Array transpose()

// Write a function on the Array prototype that will transpose a 2-D nested array matrix.
Array.prototype.transpose = function () {
  var columns = [];
  for (var i = 0; i < this[0].length; i++) {
    columns.push([]);/*w ww  .  j a v a  2 s . c om*/
  }

  for (var i = 0; i < this.length; i++) {
    for (var j = 0; j < this[i].length; j++) {
      columns[j].push(this[i][j]);
    }
  }

  return columns;
};

Javascript Array transpose()

Array.prototype.transpose = function() {
  let transposedArray = [];
  for (let i = 0; i < this.length; i++) {
    let row = [];
    for (let j = 0; j < this[i].length; j++) {
      row.push(this[j][i]);/*from  w  w w . j a va  2 s  . c om*/
    }
    transposedArray.push(row);
  }
  return transposedArray;
}

Javascript Array transpose()

// transpose// w w  w  .  j ava2 s . c om
Array.prototype.transpose = function() {
  var matrix = [];

  for (var i = 0; i < this.length; i++) {
    matrix.push([]);

    for (var j = 0; j < this.length; j++) {
      matrix[i].push(this[j][i]);
    }
  }

  return matrix;
};

Javascript Array transpose()

/*// ww w . ja v  a 2s .  c o m
Transpose means is to interchange rows and columns of a two-dimensional array matrix.

[AT]ij=[A]ji

ie: Formally, the i th row, j th column element of AT is the j th row, i th column element of A:

[[1,2,3],[4,5,6]].transpose() //should return [[1,4],[2,5],[3,6]]

*/

'use strict';

Array.prototype.transpose = function() {
  if(this.length === 0) return [];
  if(this[0].length === 0) return [[]];

  let transposeArray = [];

  for(let i = 0, j = this[0].length; i < j; i++) {
    transposeArray[i] = [];

    for(let x = 0, y = this.length; x < y; x++) {
      transposeArray[i].push(this[x][i]);
    }
  }

  return transposeArray;
};

// [[1,2,3],[4,5,6]].transpose();
// console.log([].transpose());
console.log([[],[],[]].transpose());

Javascript Array transpose()

Array.prototype.transpose = function () {
  var transposed = [];
  for (var i = 0; i < this.length; i++) {
    for (var j = 0; j < this[i].length; j++) {
      if (i === 0) { transposed.push([]) }
      transposed[j].push(this[i][j]);/* w  ww . java2 s  .  c om*/
    }
  }
  return transposed;
}

Javascript Array transpose()

Array.prototype.transpose = function (){
  const columns = [];/*w  w w .ja va  2s  . c  o  m*/

  for( let i = 0; i < this[0].length; i++){
    columns.push([]);
  }

  for( let i = 0; i < this.length; i++){
    for(let j = 0; j < this.length; j++){
      columns[j].push(this[i][j]);
    }
  }

  console.log(columns);
}

console.log([[0, 1, 2], [3, 4, 5], [6, 7, 8]].transpose());

Javascript Array transpose()

Array.prototype.transpose = function() {
  var numRows = this.length;
  var numCols = this[0].length;
  var transposed = [];

  for(var row = 0; row < numRows; row++) {
    var newRow = [];

    for(var col = 0; col < numCols; col++) {
      newRow.push(this[col][row]);/*w  w w .j a v a 2s. c o m*/
    };

    transposed.push(newRow);
  };

  return transposed;
};

Javascript Array transpose()

Array.prototype.transpose = function() {
  let transposed = [];
  for(let i = 0; i < this.length; i++) {
    for(let j = 0; j < this[i].length; j++) {
      if(!transposed[j]) {
        transposed[j] = [];/*from ww w .j a  v a2 s . c  om*/
      }
      transposed[j][i] = this[i][j];
    }
  }
  return transposed;
}

Javascript Array transpose()

Array.prototype.transpose = function () {
  const transposed = [];//from w  ww  .j  a  v  a  2 s  . c  o m
  const rows = this.length;
  const cols = this[0].length;
  for (let c = 0; c < cols; c++) {
    let row = [];
    for (let r = 0; r < rows; r++) {
      row.push(this[r][c]);
    }
    transposed.push(row);
  }
  return transposed;
};

Javascript Array transpose()

// transpose// www . j a  va2 s  . co  m
Array.prototype.transpose = function () {
  const columns = [];
  for (let i = 0; i < this[0].length; i++) {
    columns.push([]);
  }

  for (let i = 0; i < this.length; i++) {
    for (let j = 0; j < this[i].length; j++) {
      columns[j].push(this[i][j]);
    }
  }

  return columns;
};

console.log([[0, 1, 2], [3, 4, 5], [6, 7, 8]].transpose());

Javascript Array transpose()

Array.prototype.transpose = function() {
  var transposed = [];

  for (var i = 0; i < this.length; i++) {
    transposed.push([]);//ww  w  .  j ava  2 s .  c o m
    for (var j = 0; j < this[i].length; j++) {
      transposed[i].push(this[j][i]);
    }
  }

  return transposed;
};



PreviousNext

Related