Split an array into chunks in JavaScript
We have a large array and we want to split it into chunks. So these are few of the methods to do it.
- splice()
This method adds/removes items to/from an array, and returns the list of removed item(s).
Syntax:array.splice(index, number, item1, ....., itemN)
Parameters:
- index:This parameter is required. It specifies the integer at what position to add/remove items, Negative values are helpful to specify the position from the end.
- number:This parameter is optional. It specifies the number of items to be removed. 0 means, nothing to remove.
- item1, ….., itemN:This parameter is optional. This specifies the new item(s) to be added to array.
Return value:
Returns a new Array, having the removed items. - slice()
This method returns a new array containing the selected elements.
This method selects the elements starts from the given start argument, and ends at, but excluding, the given end argument.
Syntax:array.slice(start, end)
Parameters:
- start:This parameter is optional. It specifies the integer from where to start the selection (first element is at index 0). Negative numbers are used to select from the end of array. If not used, it acts like “0”
- end:This parameter is optional. It specifies the integer from where to end the selection. If not used, all elements from the start to the end of array will be included in selection. Negative numbers are used to select from the end.
Return value:
Returns a new Array, having the selected items.
Example-1:This example uses the slice() method to split the array into chunks of array. This method can be used repeatedly to split array of any size.
<!DOCTYPE html> <html> <head> <title> JavaScript | Split array into chunks. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px;"> </p> <button onclick="gfg_Run()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var i, j, chunk = 4; var array = [1, 2, 3, 4, 5, 6, 7, 8]; el_up.innerHTML = array; function gfg_Run() { var array1 = array.slice(0, chunk); var array2 = array.slice(chunk, chunk + array.length); el_down.innerHTML = array1 + ' and ' + array2; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-2: This example uses the splice() method to split the array into chunks of array. This method removes the items from the original array. This method can be used repeatedly to split array of any size.
<!DOCTYPE html> <html> <head> <title> JavaScript | Split array into chunks. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px;"> </p> <button onclick="gfg_Run()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var i, j, chunk = 2; var array = [1, 2, 3, 4, 5, 6, 7, 8]; el_up.innerHTML = array; function gfg_Run() { var array1 = array.splice(0, chunk); var array2 = array.splice(0, chunk); var array3 = array.splice(0, chunk); var array4 = array.splice(0, chunk); el_down.innerHTML = array1 + ' | ' + array2 + ' | ' + array3 + ' | ' + array4; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- JavaScript | String split()
- JavaScript | Split a string with multiple separators
- How to put string in array, split by new line in PHP ?
- Split a comma delimited string into an array in PHP
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- RMS Value Of Array in JavaScript
- JavaScript | Array pop()
- JavaScript | Array shift()
- JavaScript | Array.from() method
- JavaScript | Array unshift()
- JavaScript | Array copyWithin()
- How to empty an array in JavaScript?
- JavaScript | Array lastIndexOf()
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.



