Javascript Array my_transpose()

Description

Javascript Array my_transpose()


Array.prototype.my_transpose = function() {
  var originalArray = this;
  var numColumns = originalArray[0].length;
  var transposedArray = [];
  for (var i=0 ; i < numColumns ; i++){
    transposedArray.push([]);/*from  w w  w. j av  a2 s . c  om*/
  }
  originalArray.forEach(function(row, index1){
    row.forEach(function(element, index2){
      transposedArray[index2][index1] = element;
    })
  })
  return transposedArray;
};

Javascript Array my_transpose()

Array.prototype.my_transpose = function () {
  result = [];//from  w w w  . ja v  a  2  s. c om
  for(var x = 0; x < this[0].length; x++) {
    new_row = [];
    for(var y = 0; y < this.length; y++) {
      new_row.push(this[y][x]);
    }
    result.push(new_row);
  }
  return result;
};



PreviousNext

Related