How to use the chunk function from lodash

Find comprehensive JavaScript lodash.chunk code examples handpicked from public code repositorys.

lodash.chunk is a function in the Lodash library that splits an array into smaller arrays of a specified size.

135
136
137
138
139
140
141
142
143
144
} else if (options.albumPreviews === 'spread') {
  if (potential.length < PREVIEW_COUNT) {
    this.previews = _.slice(potential, 0, PREVIEW_COUNT)
  } else {
    const bucketSize = Math.floor(potential.length / PREVIEW_COUNT)
    const buckets = _.chunk(potential, bucketSize)
    this.previews = buckets.slice(0, PREVIEW_COUNT).map(b => b[0])
  }
} else {
  throw new Error(`Unsupported preview type: ${options.albumPreviews}`)
fork icon80
star icon649
watch icon21

+ 4 other calls in file

47
48
49
50
51
52
53
54
55
56
module.exports.capitalize          = _.capitalize;
module.exports.castArray           = _.castArray;
module.exports.cat                 = _.cat;
module.exports.ceil                = _.ceil;
module.exports.chain               = _.chain;
module.exports.chunk               = _.chunk;
module.exports.chunkAll            = _.chunkAll;
module.exports.chunkContrib        = _.chunkContrib;
module.exports.clamp               = _.clamp;
module.exports.clone               = _.clone;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.chunk work?

lodash.chunk is a function in the Lodash library that splits an array into smaller arrays of a specified size.

When called with an array and a size argument, lodash.chunk creates a new array of subarrays, each containing up to size elements from the original array. If the original array does not divide evenly into subarrays of size size, the last subarray will contain the remaining elements.

For example, if we have an array [1, 2, 3, 4, 5], and we call lodash.chunk(arr, 2), the resulting array would be [[1, 2], [3, 4], [5]]. If we called lodash.chunk(arr, 3), the resulting array would be [[1, 2, 3], [4, 5]].

The lodash.chunk function can be useful for breaking up large arrays into smaller pieces for processing or displaying data, such as in pagination or table displays.

lodash.chunk also has several optional arguments that allow for more fine-grained control over the resulting subarrays, such as the ability to pad the last subarray with a specified value, or to omit subarrays with empty values.

For example, we could call lodash.chunk with a third argument of null to pad the last subarray with null values if necessary:

javascript
const arr = [1, 2, 3, 4, 5]; const chunked = _.chunk(arr, 3, null); // chunked = [[1, 2, 3], [4, 5, null]]

Or, we could call lodash.chunk with a third argument of false to omit any subarrays with empty values:

javascript
const arr = [1, 2, 3, 4, 5]; const chunked = _.chunk(arr, 3, false); // chunked = [[1, 2, 3], [4, 5]]

In both cases, the lodash.chunk function provides greater control over the resulting subarrays, allowing us to tailor the output to our specific use case.

289
290
291
292
293
294
295
296
297
298
299
300
301


function batchEvents(eventsChunk) {
  const batchedResponseList = [];


  // arrayChunks = [[e1,e2,e3,..batchSize],[e1,e2,e3,..batchSize]..]
  const arrayChunks = _.chunk(eventsChunk, MAX_BATCH_SIZE);


  arrayChunks.forEach((chunk) => {
    const batchEventResponse = generateBatchedPayloadForArray(chunk);
    batchedResponseList.push(
fork icon77
star icon54
watch icon20

+ 2 other calls in file

1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
    eventsNotBatched = false;
  }
}
if (batchedEvents.length === 0 || eventsNotBatched) {
  if (transformedMessage.length > maxBatchSize) {
    transformedMessage = _.chunk(transformedMessage, maxBatchSize);
  }
  batchedEvents.push({
    events: transformedMessage,
    metadata: [transformedInput.metadata],
fork icon77
star icon54
watch icon20

Ai Example

1
2
3
4
5
6
7
const _ = require("lodash");

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const chunked = _.chunk(arr, 3);

console.log(chunked);
// Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

In this example, we have an array arr containing the numbers 1 through 10, and we use _.chunk(arr, 3) to split the array into smaller subarrays containing 3 elements each. The resulting chunked array contains four subarrays, with the last subarray containing only one element since the original array did not divide evenly into subarrays of size 3. We could use this technique, for example, to display the elements of the original array in groups of 3 in a web page or user interface, or to process the elements of the array in smaller chunks for performance reasons.

186
187
188
189
190
191
192
193
194
195
//    event_set_id2: [...events]
// }
const groupedEventChunks = _.groupBy(eventChunksArray, (event) => event.message.event_set_id);
Object.keys(groupedEventChunks).forEach((eventSetId) => {
  // eventChunks = [[e1,e2,e3,..batchSize],[e1,e2,e3,..batchSize]..]
  const eventChunks = _.chunk(groupedEventChunks[eventSetId], MAX_BATCH_SIZE);
  eventChunks.forEach((chunk) => {
    const batchEventResponse = generateBatch(eventSetId, chunk);
    batchedResponseList.push(
      getSuccessRespEvents(
fork icon76
star icon53
watch icon20

+ 4 other calls in file

487
488
489
490
491
492
493
494
495
496
// batching identifyArrayChunks
let identifyBatchedResponseList = [];
if (identifyEventChunks.length > 0) {
  // arrayChunks = [[e1,e2,e3,..batchSize],[e1,e2,e3,..batchSize]..]
  // transformed payload of (n) batch size
  const identifyArrayChunks = _.chunk(identifyEventChunks, IDENTIFY_MAX_BATCH_SIZE);
  identifyBatchedResponseList = batchEvents(identifyArrayChunks);
}
// batching TrackArrayChunks
let trackBatchedResponseList = [];
fork icon75
star icon53
watch icon20

330
331
332
333
334
335
336
337
338
339
340
		fullPath = fullPath.replace(process.cwd(), "");
	return fullPath;
}


function splitPage(arr, limit) {
	const allPage = _.chunk(arr, limit);
	return {
		totalPage: allPage.length,
		allPage
	};
fork icon47
star icon19
watch icon4

+ 4 other calls in file

108
109
110
111
112
113
114
115
116
117
118
119
120
const process = (event) => processEvent(event.message, event.destination);


function batchEvents(eventsChunk) {
  const batchedResponseList = [];


  const arrayChunks = _.chunk(eventsChunk, IDENTIFY_MAX_BATCH_SIZE);


  // list of chunks [ [..], [..] ]
  arrayChunks.forEach((chunk) => {
    const metadatas = [];
fork icon74
star icon52
watch icon20

992
993
994
995
996
997
998
999
1000
1001
};

const pageReturn = [];

if (items.length > 0) {
  const pagedEntities = _.chunk(items, perPage);

  for (let pageNum = 0; pageNum < pagedEntities.length; pageNum++) {
    let pagedPage = { ...page };
    if (pageNum > 0) {
fork icon7
star icon15
watch icon0

30
31
32
33
34
35
36
37
38
39
    }
    return invalidString;
}

function cat(...predicates) {
    const pairs = _.chunk(predicates, 2);

    return {
        op: 'cat',
        conform: _.partial(_conform, pairs),
fork icon1
star icon12
watch icon2

0
1
2
3
4
5
6
7
8
9
10
11
12
13
const _ = require('lodash');




// Array


const chunk = _.chunk(['a', 'b', 'c', 'd'], 2);
console.log(chunk); // => [['a', 'b'], ['c', 'd']]


const compact = _.compact([0, 1, false, 2, '', 3]);
console.log(compact); // => [1, 2, 3]
fork icon0
star icon4
watch icon0

+ 15 other calls in file

100
101
102
103
104
105
106
107
108
109
          .absoluteValue()
          .toString()
      )
    )
  }
  const chunks = chunk(orders, 15).map(c => [0, 'ox_multi', null, c])
  return chunks
},

autoMarketMake: async steps => {
fork icon3
star icon0
watch icon9

221
222
223
224
225
226
227
228
229
230
const repository = this
return new Promise(function (resolve, reject) {
  requestItems = requestItems || []
  retries = (typeof retries === 'undefined') ? 1 : retries
  let unprocessed = []
  const batches = _.chunk(requestItems, 25)
  batches.forEach(function (batch) {
    const params = {
      RequestItems: {}
    }
fork icon2
star icon13
watch icon10

+ 2 other calls in file

187
188
189
190
191
192
193
194
195
196
* @param {Object} options Extra options allowed to send with request
* @returns {Promise}
*/
 updateRecords(moduleName, data, callback, options) {
   options = options || {};
   const data_chunks = _.chunk(data, 100);
   let counter = 0;
   let response_array = [];
   return new Promise((resolve, reject) => {
     data_chunks.map((row) => {
fork icon2
star icon1
watch icon1

+ 24 other calls in file

355
356
357
358
359
360
361
362
363
364

const paramsArray = [];

// We split participants up in chunks of 20 since the MS Graph API seems to struggle with longer search queries
if (params.participants && params.participants.length > 0) {
    const participantsChunks = _.chunk(params.participants, 20);

    participantsChunks.forEach((participants) => {

        paramsArray.push({ ...params, participants });
fork icon2
star icon1
watch icon6

+ 2 other calls in file

246
247
248
249
250
251
252
253
254
}
console.log('-- Done getting BSC pool data --\n')

console.log('\n-- Getting MATIC pool data --')

const maticPoolBatches = chunk(
  pools.filter(pool => pool.chain === CHAIN_TYPES.MATIC),
  GET_POOL_DATA_BATCH_SIZE,
)
fork icon2
star icon0
watch icon0

+ 39 other calls in file

340
341
342
343
344
345
346
347
348
349
);

// init new base type, set whitelist to match controller
await setup.setDefaults({ nameOverride: nameBase });

const wlChunked = _.chunk(controllerWhitelist, 50);
for (let chunk of wlChunked) {
  //try {
  await CONST.web3_tx(
    'whitelistMany',
fork icon0
star icon2
watch icon2

40
41
42
43
44
45
46
47
48
49
50
 * @private
 * @param {string[]} funcNames The function names.
 * @returns {string} Returns the function list string.
 */
function toFuncList(funcNames) {
  let chunks = _.chunk(funcNames.slice().sort(), 5);
  let lastChunk = _.last(chunks);
  const lastName = lastChunk ? lastChunk.pop() : undefined;


  chunks = _.reject(chunks, _.isEmpty);
fork icon0
star icon0
watch icon1

+ 4 other calls in file

150
151
152
153
154
155
156
157
158
159
if (listOfBodies.length === 0) {
  // dont send message
  apiCalled.resolve({ resolve: [], reject: [] })
  return apiCalled.promise
}
const batchesOfBodies = _.chunk(listOfBodies, __constants.CHUNK_SIZE_FOR_SEND_SUCCESS_OPTIN_MESSAGE)
qalllib.qASyncWithBatch(sendOptinMessage, batchesOfBodies, __constants.BATCH_SIZE_FOR_SEND_SUCCESS_OPTIN_MESSAGE, authToken)
  .then(data => {
    if (data.reject.length) {
      return apiCalled.reject(data.reject[0])
fork icon0
star icon0
watch icon1

+ 4 other calls in file

28
29
30
31
32
33
34
35
36
37
38
39
const brightcoveVideosDataDir = FILE_DIR_PATH
const cache = {};
const functions = {};


function divideArrayInSubArrays(arr, subArrayLength) {
    return _.chunk(arr, subArrayLength);
}


function createDirectory() {
    if (!fs.existsSync(brightcoveVideosDataDir)) {
fork icon0
star icon0
watch icon1

+ 12 other calls in file

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)