Nodejs Array Chunk chunks(chunks)

Here you can find the source of chunks(chunks)

Method Source Code

Array.prototype.chunks = function(chunks) {
  'use strict';//from   ww w  .j  a va2 s.  c o  m

  if (this.length === 0 || chunks === 1) {
    return this;
  }

  var result = [];

  var itemsPerChunk = parseInt(this.length / chunks, null);
  if ((itemsPerChunk * chunks) < this.length) {
    itemsPerChunk++;
  }

  result[0] = this.splice(0, itemsPerChunk);

  var chunksRemaining = chunks - 1;
  if (this.length % chunksRemaining === 0) {
    itemsPerChunk = this.length / chunksRemaining;
  }

  for (var chunk = 1 ; chunk <= chunksRemaining ; chunk++) {
    result[chunk] = this.splice(0, itemsPerChunk);
  }

  return result;
};

Related

  1. chunk(chunkSize)
    Array.prototype.chunk = function(chunkSize) {
      var array = this;
      return [].concat.apply([],
        array.map(function(elem,i) {
            return i%chunkSize ? [] : [array.slice(i,i+chunkSize)];
        })
      );
    };
    
  2. chunk(chunkSize)
    var fs = require('fs');
    var argv = require('argv');
    var _ = require('underscore');
    var arg = argv.run().targets;
    var num = parseInt(arg[0]);
    Array.prototype.chunk = function(chunkSize) {
      var results = [];
      for (var i=0; i<this.length; i+=chunkSize)
        results.push(this.slice(i,i+chunkSize));
    ...
    
  3. chunk(n)
    Array.prototype.chunk(n) {
        var len = this.length,out = [], i = 0;
        while (i < len) {
            var size = Math.ceil((len - i) / n--);
            out.push(this.slice(i, i += size));
        return out;
    
  4. chunk(size)
    Array.prototype.chunk = function(size) {
      var chunks = [];
      for (key = 0; key < this.length; key += size) {
        chunks.push(this.slice(key, key+size));
      return chunks;
    
  5. chunk(size)
    import Vue from 'vue';
    Array.prototype.chunk = function(size) {
        let sets = [],
            a = [...this],
            chunk = a.length / size;
        while (sets.length < chunk) {
            sets[sets.length] = a.splice(0, size);
        return sets;
    ...