Changing the Array Prototype in JavaScript

I'm trying to change the Array Prototype and access columns as if they are rows therefor doing a custom matrix transposition. I want to make sure that the access of each column doesn't require re-allocating and creating an entirely new object just to access a particular column.

For example...

var mcol = new column([[0,1,2],[3,4,5],[6,7,8]]);

What I'm looking for is to read the column as if it was a row... (doing a 90 degree transform on the matrix)

mcol[1] = [1,4,7];

Any suggestions?


You can use this constructor:

function column() {
    var arr =;

    return arr;

Allowing you to do what you want.

var mcol = new column([0,1,2], [3,4,5], [6,7,8]);
alert(mcol[1]); // alerts [3, 4, 5]

I'd suggest you make a Matrix constructor with a column method:

function Matrix() {
  this._rows =;
Matrix.prototype.column = function (i) {
  // See (1)
  return (row) {
    return row[i];

var m = new Matrix([0,1,2],[3,4,5],[6,7,8]);
console.log(m.column(1)); // [1,4,7]


The solution I have is a work around and involves copying the array to a new array which I don't like. It does override the Array prototype.

    Array.prototype.rotate = function () {
      var rotated = [];
      var columns = 1; 
      for(var col = 0; col < columns; col++) {
        var myrow = [];
        for(var row = 0; row < this.length; row++){ // this.length is the longest column
          if(this[row].length > columns){
            columns = this[row].length;
          if(this[row].length > col){
          } else {
      return rotated;

var mcol = [[0,1,2], [3,4,5], [6,7,8]];
mcol = mcol.rotate();

Alerts [1,4,7]

Does anyone know of a solution that doesn't require someone to copy the entire array to a new array?

Need Your Help

Voronoi graph from set of polygons in Emgu CV (or OpenCV)

opencv emgucv polygons voronoi

Using Emgu CV I have extracted a set of closed polygons from the contours in an image of a road network. The polygons represent road outlines. The result is shown below, plotted over an OpenStreetM...

Image resize with window, based on set pixel margins?

html css image resize margins

I'm trying to get an image centered on the screen, and I want it to stretch horizontally.