Split Array into Smaller Chunks

Split Array into Smaller Chunks


In this example, you will learn to write a JavaScript program that will split an array into smaller chunks of array.

To understand this example, you should have the knowledge of the following JavaScript programming topics:


JavaScript Array slice()

JavaScript Array splice()

JavaScript Array


Example 1: Split Array Using slice()



In the above program, the for loop is used with the slice() method to split an array into smaller chunks of array.


The for loop iterates through the elements of an array. During each iteration, the value of i is increased by chunk value (here 2).


The slice() method extracts elements from an array where:


The first argument specifies the starting index.

The second argument specifies the ending index.


Example 2: Split Array Using splice()



In the above program, the while loop is used with the splice() method to split an array into smaller chunks of an array.


In the splice() method,


The first argument specifies the index where you want to split an item.

The second argument (here 2) specifies the number of items to split.

The while loop is used to iterate over the array until the array is empty.


Post a Comment

Previous Post Next Post