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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.