Lodash – chunk method

  • Post author:
  • Post category:Lodash
  • Post comments:2 Comments
Lodash  chunk method

Syntax Of Chunk Method

_.chunk(array, [size=1])

Creates an array of elements split into groups the length of size. If the array can’t be split evenly, the final Lodash chunk method will be the remaining elements.

Arguments

  • array (Array) โˆ’ The array to process.
  • [size=1] (number) โˆ’ The length of each chunk.

Output

  • (Array) โˆ’ Returns the new array of chunks.

Example

var _ = require('lodash');
var numbers = [1, 2, 3, 4];
var listOfNumbers = '';

listOfNumbers = _.chunk(numbers, 2);
console.log(listOfNumbers);

listOfNumbers = _.chunk(numbers, 3);
console.log(listOfNumbers);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

[ [ 1, 2 ], [ 3, 4 ] ]
[ [ 1, 2, 3 ], [ 4 ] ]

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply