The chunk() function breaks the collection to many small collections of the given size. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
Syntax:
data.chunk(x)
Parameter: This function accepts a single parameter as mentioned above and described below.
- x : This parameter holds an integer number that defines where to break the collection
Return value: Return the sub collection of the main collection
Below examples illustrate the chunk() function in collect.js:
Example 1: Here in this example, we take a collection and then using the chunk() function we divide the collection.
Javascript
// It is used to import collect.js library const collect = require('collect.js'); const collection = collect([1, 2, 3, 4, 5, 6, 7]); const x = collection.chunk(5); console.log(x.all()); |
Output :
[ Collection { items: [ 1, 2, 3, 4, 5 ] },
Collection { items: [ 6, 7 ] } ]
Example 2: In this example, we will pass two values in the chunk() function.
Javascript
// It is used to import collect.js library const collect = require('collect.js'); const collection = collect([0 , 1 , 2 , 3 , 4 , 5 , 6 ]); const x = collection.chunk(2, 4); console.log(x.all()); |
Output:
[ Collection { items: [ 0, 1 ] },
Collection { items: [ 2, 3 ] },
Collection { items: [ 4, 5 ] },
Collection { items: [ 6 ] } ]
Recommended Posts:
- Underscore.js | _.chunk()
- Lodash _.chunk() Method
- How to get the function name from within that function using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- PHP | imagecreatetruecolor() Function
- p5.js | year() function
- D3.js | d3.utcTuesdays() Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- D3.js | d3.map.set() Function
- D3.js easePolyOut Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- D3.js | d3.bisectLeft() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : akhilsharma870


