How to Chunk an Array with JavaScript
In this article, we’ll explore another common interview question for JavaScript developers – Chunk an Array. Now, like all of these interview questions, this one is not so complicated. Make sure to study the examples before attempting to solve the problem
Introduction
We’re going to start off with the following function:
function chunk(arr, size) {
// Your code here
}
Using the two arguments, we want to divide the array into subarrays where each sub array is of length size. Put simply, we’re taking one big array and splitting it into many smaller sub arrays that are all contained within one larger array.
Examples & Directions
Directions: Given an array and chunk size, divide the array into many subarrays where each subarray is of length size
Examples:
chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
Looking at the examples, you should notice a pattern. Notice that the number passed in after the array is specifying how many elemnts go into each subarray. It’s not specifying the total number of chunks, just the amount of elements that go the chunk.
For example, look at 1. chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
. The specifier is the number 2
after the array [1, 2, 3, 4]
.
Take a look at example 2. chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
. Notice there’s not enough elements in the array to make three subarrays with 2 elements in all of them. The loner, 5
, will be stored within it’s on chunk. Likewise, examples 3, 4, and 5 mimic this same behavior. Therefore, if you have a lone element, simply store it in it’s own chunk.
NOTE: Try to solve the problem on your own before viewing the solutions.
Solution 1
function chunk(array, size) {
const chunked = [];
let index = 0;
while (index < array.length) {
chunked.push(array.slice(index, index + size));
index += size;
}
return chunked;
}
console.log(chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]);
Solution 2
function chunk(array, size) {
const chunked = [];
for (let element of array) {
const last = chunked[chunked.length - 1];
if (!last || last.length === size) {
chunked.push([element]);
} else {
last.push(element);
}
}
return chunked;
}
console.log(chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]);
Related Posts
The Great JavaScript Debate: To Semicolon or Not?
Since I’ve started learning this language, JavaScript has undergone some heavy changes. Most notably, it seems to be the norm to not use semicolons anymore.
Read moreHacktoberfest 2024: Get a Free JavaScript Today Sticker
October is here, and that means one thing in the tech world—Hacktoberfest! This annual event, powered by DigitalOcean, Cloudflare, Quira, and other sponsors, encourages developers of all skill levels to contribute to open-source projects.
Read moreCreating a Real Time Chat Application with React, Node, and TailwindCSS
In this tutorial, we will show you how to build a real-time chat application using React and Vite,as well as a simple Node backend.
Read more