Partitioning Arrays with JavaScript Reduce

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to partition an array based on a provided function using JavaScript. We will use the Array.prototype.reduce() method to accumulate the partitioned values into an array while checking for changes in the output of the provided function. By the end of this lab, you will have a solid understanding of how to partition arrays in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28541{{"`Partitioning Arrays with JavaScript Reduce`"}} javascript/data_types -.-> lab-28541{{"`Partitioning Arrays with JavaScript Reduce`"}} javascript/arith_ops -.-> lab-28541{{"`Partitioning Arrays with JavaScript Reduce`"}} javascript/comp_ops -.-> lab-28541{{"`Partitioning Arrays with JavaScript Reduce`"}} javascript/cond_stmts -.-> lab-28541{{"`Partitioning Arrays with JavaScript Reduce`"}} javascript/higher_funcs -.-> lab-28541{{"`Partitioning Arrays with JavaScript Reduce`"}} javascript/destr_assign -.-> lab-28541{{"`Partitioning Arrays with JavaScript Reduce`"}} end

Partition Array Algorithm

To partition an array, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Apply the provided function fn to each value in the given array arr.
  3. Split the array each time fn returns a new value.
  4. Use Array.prototype.reduce() to create an accumulator object that holds the resulting array and the last value returned from fn.
  5. Use Array.prototype.push() to add each value in arr to the appropriate partition in the accumulator array.
  6. Return the resulting array.

Here is the code implementation:

const partitionBy = (arr, fn) =>
  arr.reduce(
    ({ res, last }, v, i, a) => {
      const next = fn(v, i, a);
      if (next !== last) res.push([v]);
      else res[res.length - 1].push(v);
      return { res, last: next };
    },
    { res: [] }
  ).res;

Example usage:

const numbers = [1, 1, 3, 3, 4, 5, 5, 5];
partitionBy(numbers, (n) => n % 2 === 0); // [[1, 1, 3, 3], [4], [5, 5, 5]]
partitionBy(numbers, (n) => n); // [[1, 1], [3, 3], [4], [5, 5, 5]]

Summary

Congratulations! You have completed the Partition Array lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like