Question: I am just learning javascript.I am pretty confused with manipulating multidimensional arrays.

```
var myarr = [ 
       [ 7, 9, 10 ], 
       [ 6, 9 ], 
       [ 5, 9 ] 
  ]

```

I want to insert zero like this.What is the smart way to do this in javascript

```
       [ 
           [ 7, 9, 10 ], 
           [ 6, 9, 0 ], 
           [ 5, 9, 0 ] 
       ]

```

Answer: 
You could get first the max length of the inner arrays and push zeroes until all inner arrays have the same length.

```js
var myarr = [[7, 9, 10], [6, 9], [5, 9]],
    length = myarr.reduce(function (r, a) { return Math.max(r, a.length); }, 0);

myarr.forEach(function (a) {
    while (a.length < length) {
        a.push(0);
    };
});

console.log(myarr);
```

```css
.as-console-wrapper { max-height: 100% !important; top: 0; }
```
Seems like you need a square matrix from a 2 dimension array, so try this:

```js
var myarr = [
  [7, 9, 10],
  [6, 9],
  [5, 9, 1, 2, 3, 4, 5, 6, 7] // <--- for example purpose
];

var rows = myarr.length;
var cols = 0;

for(var i = 0; i < myarr.length; i++){
  if(myarr[i].length > cols)
    cols = myarr[i].length;
}

var limit = rows > cols ? rows : cols;
for(var i = 0; i < limit; i++){
  if(myarr[i] == undefined)
    myarr[i] = new Array(limit);
  for(var j = 0; j < limit; j++){
    if(typeof myarr[i][j] == "undefined")
      myarr[i][j] = 0;
  }
}

for(var i = 0; i < limit; i++){
  console.log(JSON.stringify(myarr[i]));
}
```