title: Multi dimensional array cumulative sum in Javascript
body: <p>Given a multi-dimensional array:</p>
  
  <pre><code>var a = [[3,2,5], [4,1,7], [1,6,8]];
  </code></pre>
  
  <p>I would like to do a cumulative sum of each array to return the following result:</p>
  
  <pre><code>[[3,2,5], [7,3,12], [8,9,20]];
  </code></pre>
  
  <ul>
  <li>cum sum on 1st elements of each sub-array: 3 4 1</li>
  <li>cum sum on 2nd elements of each sub-array: 2 1 6</li>
  <li>cum sum on 3rd elements of each sub-array: 5 7 8</li>
  </ul>
  
  <p>I've tried using reduce(), but can't quite get the expected result.</p>
  
  <p>Any suggestions, much appreciated.</p>
  
  <p>S</p>
  
  <p><strong>UPDATE -</strong>
  Taking it to the next level:</p>
  
  <pre><code>var a = [
      [new Date(), 3,2,5], 
      [new Date(), null,1,7], 
      [new Date(), null,6,8], 
      [new Date(), 1,2,3]
  ];
  </code></pre>
  
  <p>Should result in:</p>
  
  <pre><code>[[new Date(), 3,2,5], 
   [new Date(), null,3,12],
   [new Date(), null,9,20],
   [new Date(), 4,11,23]]
  </code></pre>
  
  <p>My approach was to create a multi-dimensional offsetIndex array:</p>
  
  <pre><code>var offsetIdx = [];
          for (var i=1; i&lt;a.length; i++) {
  
              for (var z=0; z&lt;a[i].length; z++) {
                  var zValue = a[i][z];
  
                  oIdx = offsetIdx[z] || 0;
  
                  a[i][z] = zValue &amp;&amp; z!==0 ? a[i-1-oIdx][z] + zValue : zValue;
  
                  if(!zValue){
                      offsetIdx[z] = oIdx + 1;
                  } else {
                      offsetIdx[z] = 0; 
                  }
              }
          }
  </code></pre>
  
  <p>Happy to see other approaches and ways of making it ultra light-weight.</p>