Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Modified from an answer by dbaseman: <a href="https://stackoverflow.com/a/10456344/711085">https://stackoverflow.com/a/10456344/711085</a></p> <pre><code>Object.defineProperty(Array.prototype, 'chunk_inefficient', { value: function(chunkSize) { var array=this; return [].concat.apply([], array.map(function(elem,i) { return i%chunkSize ? [] : [array.slice(i,i+chunkSize)]; }) ); } }); </code></pre> <p>Demo:</p> <pre><code>&gt; [1,2,3,4,5,6,7].chunk_inefficient(3) [[1,2,3],[4,5,6],[7]] </code></pre> <hr> <p><em>minor addendum</em>:</p> <p>I should point out that the above is a not-that-elegant (in my mind) workaround to use <code>Array.map</code>. It basically does the following, where ~ is concatenation:</p> <pre><code>[[1,2,3]]~[]~[]~[] ~ [[4,5,6]]~[]~[]~[] ~ [[7]] </code></pre> <p>It has the same asymptotic running time as the method below, but perhaps a worse constant factor due to building empty lists. One could rewrite this as follows (mostly the same as Blazemonger's method, which is why I did not originally submit this answer):</p> <p>More efficient method:</p> <pre><code>Object.defineProperty(Array.prototype, 'chunk', { value: function(chunkSize) { var R = []; for (var i=0; i&lt;this.length; i+=chunkSize) R.push(this.slice(i,i+chunkSize)); return R; } }); // refresh page if experimenting and you already defined Array.prototype.chunk </code></pre> <hr> <p>My preferred way nowadays is the above, or one of the following:</p> <pre><code>Array.range = function(n) { // Array.range(5) --&gt; [0,1,2,3,4] return Array.apply(null,Array(n)).map((x,i) =&gt; i) }; Object.defineProperty(Array.prototype, 'chunk', { value: function(n) { // ACTUAL CODE FOR CHUNKING ARRAY: return Array.range(Math.ceil(this.length/n)).map((x,i) =&gt; this.slice(i*n,i*n+n)); } }); </code></pre> <p>Demo:</p> <pre><code>&gt; JSON.stringify( Array.range(10).chunk(3) ); [[1,2,3],[4,5,6],[7,8,9],[10]] </code></pre> <p>Or if you don't want an Array.range function, it's actually just a one-liner (excluding the fluff):</p> <pre><code>var ceil = Math.ceil; Object.defineProperty(Array.prototype, 'chunk', {value: function(n) { return Array(ceil(this.length/n)).fill().map((_,i) =&gt; this.slice(i*n,i*n+n)); }}); </code></pre> <p>or</p> <pre><code>Object.defineProperty(Array.prototype, 'chunk', {value: function(n) { return Array.from(Array(ceil(this.length/n)), (_,i)=&gt;this.slice(i*n,i*n+n)); }}); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload