title: Splitting a JS array into N arrays
body: <p>Imagine I have an JS array like this:</p>
  
  <pre><code>var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  </code></pre>
  
  <p>What I want is to split that array into N smaller arrays. For instance:</p>
  
  <pre><code>split_list_in_n(a, 2)
  [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11]]
  
  For N = 3:
  [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11]]
  
  For N = 4:
  [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]
  
  For N = 5:
  [[1, 2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]
  </code></pre>
  
  <p>For Python, I have this:</p>
  
  <pre><code>def split_list_in_n(l, cols):
      """ Split up a list in n lists evenly size chuncks """
      start = 0
      for i in xrange(cols):
          stop = start + len(l[i::cols])
          yield l[start:stop]
          start = stop
  </code></pre>
  
  <p>For JS, the best right solution that I could come up with is a recursive function, but I don't like it because it's complicated and ugly. This inner function returns an array like this [1, 2, 3, null, 4, 5, 6, null, 7, 8], and then I have to loop it again and split it manually. (My first attempt was returning this: [1, 2, 3, [4, 5, 6, [7, 8, 9]]], and I decided to do it with the null separator).</p>
  
  <pre><code>function split(array, cols) {
      if (cols==1) return array;
      var size = Math.ceil(array.length / cols);
      return array.slice(0, size).concat([null]).concat(split(array.slice(size), cols-1));
  }
  </code></pre>
  
  <p>Here's a jsfiddle of that: <a href="http://jsfiddle.net/uduhH/" rel="noreferrer">http://jsfiddle.net/uduhH/</a></p>
  
  <p>How would you do that? Thanks!</p>