Chunking array within array in JavaScript


In this problem statement, our target is to get the chunking array within the array with the help of Javascript functionalities. So basically we will divide an array into smaller arrays.

What is the meaning of Chunking array within an array ?

Chunking an array means arranging the array items in smaller subarrays of the same size. Or we can say that chunking an array within an array refers to the process of dividing the large size array into the smaller subarrays or chunks. So the items of the original array are arranged together into the nested arrays instead of having a single flat array.

Let's consider an array: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] if we have chunk this array with a size of chunk size of 2 so the output array will be: [ ['A', 'B'], ['C', 'D'], ['E', 'F'], ['G', 'H'] ]

We can see here the original array has been divided into four chunks of size 2. In this every chunk represents a separate array within the larger array. The chunk size represents the number of items present in the subarray.

Understanding the problem

As we have seen, what is chunking an array within an array ! So our task is to divide the array into smaller subarrays of chunks as we have seen above. We can change the size of the chunks as per our requirements and it also depends on the original size of the array.

Logic for the given problem

As we have to define a function for making the chunks of the given array. So we will define a function and pass two parameters inside the function. First parameter will be an array which is to be chunked and the size which will represent the desired size of every chunk.

We will create a blank chunked array to store the resulting chunks and initialize its value with zero. The function will also use a while loop to iterate the original array. In every iteration we will slice a portion of the array from the current index and the size. And then add it to the chunked array. After that we will increase the index by size to move to the next chunk.

Algorithm

Step 1: Starting point of this algorithm is to declare a function to perform the task of chunking array within array. This function takes two arguments: array and chunk size.

Step 2: Declare a blank array to store the chunked array after performing the iteration.

Step 3: With the help or a while loop we will iterate through the array items. Inside this loop we will use the slice method to slice the array with the size of the given chunk size from the starting index. Adn increment the index value with the size of the chunks.

Step 4: Store the chunked array in the array we have defined in the second step. And show the output to the console.

Code for the above algorithm

Example

//Function to get the chunking array
function chunkArray(arr, size) {
   const chunkedArr = [];
   let index = 0;

   while (index < arr.length) {
      chunkedArr.push(arr.slice(index, index + size));
      index += size;
   }
   return chunkedArr;
}
const originalArray = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
const chunkSize = 3;

const chunkedArray = chunkArray(originalArray, chunkSize);
console.log(chunkedArray);

Output

[ [ 11, 12, 13 ], [ 14, 15, 16 ], [ 17, 18, 19 ], [ 20 ] ]

Complexity

As we need to iterate the items once with the help of a while loop to chunk the given array so the time taken by the function to generate the chunked array is O(n). Here n is the length of the provided array. And the space complexity for storing the chunked array is also O(n), because we are storing all the items in the new array which is the same length as the input array.

Conclusion

We have given a straightforward way to chunk the given array in the code. The function accepts two parameters like array and chunk size as inputs. And returns a new array containing the chunks of the items. The function has a linear time complexity.

Updated on: 11-Aug-2023

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements