Node.js lodash chunk() split array by size

Description

Node.js lodash chunk() split array by size


const lodash = require('lodash');

var _ = {};//w  w  w. j  ava 2s  . c o m

_.chunk = function (arr, size) {
  if (typeof size === 'undefined') return arr;

  var results = [];
  for (var i = 0; i < arr.length; i += size) {
    results.push(arr.slice(i, i + size));
  }
  return results;
};

console.log(lodash.chunk(['a', 'b', 'c', 'd'], 2));
console.log(_.chunk(['a', 'b', 'c', 'd'], 2));

console.log(lodash.chunk(['a', 'b', 'c', 'd'], 3));
console.log(_.chunk(['a', 'b', 'c', 'd'], 3));



PreviousNext

Related