Nodejs Array Transpose transpose()

Here you can find the source of transpose()

Method Source Code

util = require('util');

// some useful functions

var identity = function(x) {
  return x;//from   w w  w  .j a v  a2 s.c o m
};

var pair = function(a, b){
  return [a, b];
};

var isArray = function(a) {
  // from Crockford's book, p 61
  return a && typeof a === 'object' && a.constructor === Array;
};


// Array extensions

Array.prototype.transpose = function() {
  var t = [], n = this.length;
  var m = 0;
  for (var i=0; i < n; i++) {
    var col = this[i];
    if (!isArray(col)) { throw {
      name: 'invalidArgument',
      message: 'transpose method can only be applied to arrays of arrays'
    };}
    if (m == 0) {
      m = col.length;
    }
    else {
      if (m != col.length) {throw {
        name: 'invalidArgument',
        message: 'transpose method can only be applied to arrays of arrays all the same length'
      };}
    }
  }
  for (i=0; i < m; i++) {
    t[i] = [];
    for (var j=0; j < n; j++) {
      t[i][j] = this[j][i];
    }
  }
  return t;
};

Array.prototype.zip = function(a, f) {
  var combine = f || pair;
  if (this.length != a.length) {
    throw {
      name: 'invalidArgument',
      message: 'zip method can only be applied to an array of the same length as its receiver'
    };
  }
  var z = [];
  for (var i=0; i < this.length; i++) {
    z[i] = combine(this[i], a[i]);
  }
  return z;
};

Array.prototype.flatten = function() {
  var flattenIt = function(el) {
    return isArray(el)? el.flatten(): el;
  }
  return this.reduce(function(f, el, i, a) { return f.concat(flattenIt(el)); }, []);
};

Related

  1. transpose()
    Array.prototype.transpose = function() {
      var newArr = []
      for (var i = 0; i <= 2; i++) {
        newArr.push([]);
        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())
    
  2. 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;
    ...
    
  3. 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;
    };
    
  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++) {
    ...
    
  5. transpose()
    Array.prototype.transpose = function () {
      var transpose = [];
      for (var i = 0; i < this[0].length; i++) {
        var row = [];
        for (var j = 0; j < this.length; j++) {
          row.push(this[j][i]);
        transpose.push(row);
      return transpose;
    };
    
  6. transpose()
    Array.prototype.transpose = function () {
      const transposed = [];
      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;
    };
    const arr1 = [1, 2, 2, 4, 6, 6];
    const arr2 = [1, 0, -1, 2, -2];
    const mat = [[1,2,3], [4,5,6]];
    
  7. transpose()
    '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]);
    ...