Nodejs Array Transpose transpose()

Here you can find the source of transpose()

Method Source Code

//------------------------------------------------------------------------------
// transpose.js: Transpose a multi-dimensional array
// This file is a part of pk-core-js library
// Copyright (c) Alexander Gladysh <ag@logiceditor.com>
// Copyright (c) Dmitry Potapov <dp@logiceditor.com>
// See file `COPYRIGHT` for the license
//------------------------------------------------------------------------------

/**/*w w  w.j ava 2s .c  o m*/
 * Method to transpose a multi-dimensional array
 */
Array.prototype.transpose = function() {
  var result = [];

  if (!(this instanceof Array))
  {
    return result;
  }
  if (this.length == 0)
  {
    return result;
  }
  if (!(this[0] instanceof Array))
  {
    return result;
  }
  if (this[0].length == 0)
  {
    return result;
  }

  for(var i = 0; i < this[0].length; i++)
  {
    var row = [];
    for(var n = 0; n < this.length; n++)
    {
      row[n] = this[n][i];
    }
    result[i] = row;
  }

  return result;
};

/**
 * JQuery plugin definition
 */
if(typeof(jQuery) !== "undefined")
{
  jQuery.transpose = function (arr)
  {
    if(arr instanceof Array)
    {
      return arr.transpose();
    }
  };
}

Related

  1. transpose()
    Array.prototype.transpose = function() {
      for(let i = 0; i < this.length; i++){
        for(let j = 0; j < i; j++){
          let temp = this[i][j];
          this[i][j] = this[j][i];
          this[j][i] = temp;
      return this;
    ...
    
  2. 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++) {
    ...
    
  3. transpose()
    "use strict";
    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.length; j++){
          columns[j].push(this[i][j]);
    ...
    
  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 (){
      const cols = this[0].length;
      const rows = this.length;
      let result = Array(rows).fill().map(() => Array());
      this.forEach(function (row){
        row.forEach(function (num, index){
          result[index].push(num);
        });
      });
    ...
    
  6. 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++) {
    ...