How to Chunk an Array with JavaScript
Javascript Algorithms

How to Chunk an Array with JavaScript

1 min read

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:

  1. chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
  2. chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
  3. chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
  4. chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
  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]]);

About the Author

Matt Fay

Matt Fay

Matt is the founder of JavaScript Today, a platform dedicated to high-quality JavaScript education and commentary. With a deep curiosity for technology, he gravitates toward understanding how things work and has been diving into the world of information security. Outside of coding, Matt enjoys exploring languages; he can understand some Russian and Italian, and is currently learning Thai.

Discussion (Loading...)

Join the conversation

Join the Discussion

Sign in to share your thoughts and engage with the JavaScript Today community.

Loading comments...

Related Articles

Continue your learning journey with these related posts

Never Miss a JavaScript Update

Join our community of developers and receive insightful articles and coding tips every week.