Nodejs Array Transpose transpose()

Here you can find the source of transpose()

Method Source Code

Array.prototype.transpose = function() {
  var newArr = []
  for (var i = 0; i <= 2; i++) {
    newArr.push([]);/*w ww.j  a  v  a 2  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())

Related

  1. 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;
        };
      };
      return transposed;
    ...
    
  2. transpose()
    Array.prototype.transpose = function () {
      var columns = [];
      for (var i = 0; i < this[0].length; i++) {
        columns.push([]);
      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;
    };
    
  3. transpose()
    util = require('util');
    var identity = function(x) {
      return x;
    };
    var pair = function(a, b){
      return [a, b];
    };
    var isArray = function(a) {
      return a && typeof a === 'object' && a.constructor === Array;
    ...
    
  4. transpose()
    Array.prototype.transpose = function() {
      var a = this,
        w = a.length ? a.length : 0,
        h = a[0] instanceof Array ? a[0].length : 0;
      if(h === 0 || w === 0) { return []; }
      var i, j, t = [];
      for(i=0; i<h; i++) {
        t[i] = [];
        for(j=0; j<w; j++) {
    ...