Pull Matching Values From Array

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore how to pull matching values from an array and return the removed elements. We will learn how to use the Array.prototype.filter(), Array.prototype.includes(), and Array.prototype.push() methods to filter out the values specified in the pull array and mutate the original array. By the end of this lab, you will have a better understanding of how to manipulate 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/obj_manip("`Object Manipulation`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") subgraph Lab Skills javascript/variables -.-> lab-28560{{"`Pull Matching Values From Array`"}} javascript/data_types -.-> lab-28560{{"`Pull Matching Values From Array`"}} javascript/arith_ops -.-> lab-28560{{"`Pull Matching Values From Array`"}} javascript/comp_ops -.-> lab-28560{{"`Pull Matching Values From Array`"}} javascript/obj_manip -.-> lab-28560{{"`Pull Matching Values From Array`"}} javascript/higher_funcs -.-> lab-28560{{"`Pull Matching Values From Array`"}} end

How to Pull Matching Values From an Array

To pull out specific values from an array using JavaScript, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Array.prototype.filter() and Array.prototype.includes() to filter out the values that are not needed and create a new array.
  3. Set Array.prototype.length to mutate the original array by resetting its length to 0.
  4. Use Array.prototype.push() to re-populate the original array with only the pulled values.
  5. Use Array.prototype.push() to keep track of the removed values in a new array.

Here's an example function that implements these steps:

const pullAtValue = (arr, pullArr) => {
  let removed = [],
    pushToRemove = arr.forEach((v, i) =>
      pullArr.includes(v) ? removed.push(v) : v
    ),
    mutateTo = arr.filter((v, i) => !pullArr.includes(v));
  arr.length = 0;
  mutateTo.forEach((v) => arr.push(v));
  return removed;
};

You can use this function to remove specific values from an array and return the removed elements like this:

let myArray = ["a", "b", "c", "d"];
let pulled = pullAtValue(myArray, ["b", "d"]);
// myArray = [ 'a', 'c' ] , pulled = [ 'b', 'd' ]

Summary

Congratulations! You have completed the Pull Matching Values From Array lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like