Pull 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 manipulate arrays in JavaScript by creating a function that filters out specified values from an array and mutates the original array. We will make use of built-in Array methods such as filter(), includes(), and push() to achieve this functionality. 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/array_methods("`Array Methods`") javascript/BasicConceptsGroup -.-> javascript/obj_manip("`Object Manipulation`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28563{{"`Pull Values From Array`"}} javascript/data_types -.-> lab-28563{{"`Pull Values From Array`"}} javascript/arith_ops -.-> lab-28563{{"`Pull Values From Array`"}} javascript/comp_ops -.-> lab-28563{{"`Pull Values From Array`"}} javascript/array_methods -.-> lab-28563{{"`Pull Values From Array`"}} javascript/obj_manip -.-> lab-28563{{"`Pull Values From Array`"}} javascript/higher_funcs -.-> lab-28563{{"`Pull Values From Array`"}} javascript/spread_rest -.-> lab-28563{{"`Pull Values From Array`"}} end

How to Pull Values from an Array in JavaScript

To extract specific values from an array in JavaScript, you can use the Array.prototype.filter() and Array.prototype.includes() methods. Here's how you can do it:

const pull = (arr, ...args) => {
  let argState = Array.isArray(args[0]) ? args[0] : args;
  let pulled = arr.filter((v) => !argState.includes(v));
  arr.length = 0;
  pulled.forEach((v) => arr.push(v));
};

The pull function takes an array and one or more arguments that represent the values to be removed. The function then creates a new array by filtering out the values specified using Array.prototype.filter(). It then mutates the original array by resetting its length to 0 and re-populating it with only the pulled values using Array.prototype.push().

Here's an example of how you can use the pull function:

let myArray = ["a", "b", "c", "a", "b", "c"];
pull(myArray, "a", "c"); // myArray = [ 'b', 'b' ]

In this example, the pull function removes all occurrences of 'a' and 'c' from the myArray array and returns a new array with only the values 'b' and 'b'.

Summary

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

Other JavaScript Tutorials you may like