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; }
```
Best way would be first to calculate what the longest sub-array length you have, then you can pad the sub-arrays to make them all up to the same length.

The following includes a function for calculating the longest length, and another for performing the padding:

```js
function getMax(arr) {
  var max = 0;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i].length > max)
      max = arr[i].length;
  }
  return max;
}

function pad(arr, max) {
  for (var i = 0; i < arr.length; i++) {
    for (var j = arr[i].length; j < max; j++)
      arr[i].push(0);
  }
}

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

pad(myarr, getMax(myarr));

console.log(myarr);
```