Nodejs Array Transpose transpose()

Here you can find the source of transpose()

Method Source Code

"use strict";//from  ww w .  ja v  a 2 s.  c o 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.length; j++){
      columns[j].push(this[i][j]);
    }
  }

  console.log(columns);
}

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

Array.prototype.uniq = function() {
  const arr = this.filter( function( item, index, inputArray ) {
             return inputArray.indexOf(item) == index;
  });
  return arr;
};
console.log([1, 1, 2, 2, 3, 3, 4, 4, 5, 5].uniq());

Array.prototype.twoSum = function () {
  const pairs = [];

  for (let i = 0; i < this.length; i++){
    for (let j = (i + 1); j < this.length; j++){
      if(this[i] + this[j] === 0){
        pairs.push([i , j]);
      }
    }
  }

  return pairs;
};
console.log([-1, 0, 2, -2, 1].twoSum());

Related

  1. transpose()
    Array.prototype.transpose = function() {
      var new_matrix = [];
      for (var i = 0; i < this.length; i++) {
        new_matrix.push([]);
      for (var i=0; i < this.length; i++) {
        for (var j=0; j < this.length; j++) {
          new_matrix[j][i] = this[i][j];
      return new_matrix;
    };
    
  2. transpose()
    Array.prototype.transpose = function () {
      var transposed = [];
      for (var i = 0; i < this.length; i++) {
        transposed[i] = this[i].slice();
      for (var i = 0; i < this.length; i++) {
        for (var j = 0; j < this.length; j++) {
          transposed[j][i] = this[i][j];
        };
    ...
    
  3. transpose()
    Array.prototype.transpose = function () {
      var result = []
      for (var row = 0; row < this.length; row++) {
        for (var col = 0; col < this[0].length; col++) {
          if (!result[col]) {
            result[col] = [this[row][col]]
          } else {
            result[col].push(this[row][col])
      return result;
    
  4. 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;
    ...
    
  5. 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++) {
    ...
    
  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++) {
    ...
    
  7. 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);
        });
      });
    ...
    
  8. transpose()
    Array.prototype.transpose = function() {
      var result = [];
      if (!(this instanceof Array))
        return result;
      if (this.length == 0)
        return result;
    ...
    
  9. 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++) {
    ...