Partition Array in Two

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 into two separate arrays based on a provided function's truthiness for each element using JavaScript. We will use the Array.prototype.reduce() method to create two arrays and the Array.prototype.push() method to add elements to the appropriate array based on the provided function's truthiness. By the end of this lab, you will have a strong understanding of how to partition an array in JavaScript and be able to apply this knowledge in your future projects.


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/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28540{{"`Partition Array in Two`"}} javascript/data_types -.-> lab-28540{{"`Partition Array in Two`"}} javascript/arith_ops -.-> lab-28540{{"`Partition Array in Two`"}} javascript/comp_ops -.-> lab-28540{{"`Partition Array in Two`"}} javascript/higher_funcs -.-> lab-28540{{"`Partition Array in Two`"}} javascript/destr_assign -.-> lab-28540{{"`Partition Array in Two`"}} end

How to Partition an Array into Two Based on a Function

To partition an array into two based on a provided function, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Array.prototype.reduce() to create an array of two arrays.
  3. Use Array.prototype.push() to add elements for which fn returns true to the first array and elements for which fn returns false to the second one.

Here's the code you can use:

const partition = (arr, fn) =>
  arr.reduce(
    (acc, val, i, arr) => {
      acc[fn(val, i, arr) ? 0 : 1].push(val);
      return acc;
    },
    [[], []]
  );

To test this code, you can use the following example:

const users = [
  { user: "barney", age: 36, active: false },
  { user: "fred", age: 40, active: true }
];
partition(users, (o) => o.active);
// [
//   [{ user: 'fred', age: 40, active: true }],
//   [{ user: 'barney', age: 36, active: false }]
// ]

This will return an array of two arrays, where the first array contains all the elements for which the provided function returns true, and the second array contains all the elements for which the provided function returns false.

Summary

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

Other JavaScript Tutorials you may like