Pull Values From Array at Index

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore how to pull values from an array at specific indexes using JavaScript. We will be using the pullAtIndex() function, which mutates the original array to filter out the values at the specified indexes and returns the removed elements. By the end of this lab, you will have a solid 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-28561{{"`Pull Values From Array at Index`"}} javascript/data_types -.-> lab-28561{{"`Pull Values From Array at Index`"}} javascript/arith_ops -.-> lab-28561{{"`Pull Values From Array at Index`"}} javascript/comp_ops -.-> lab-28561{{"`Pull Values From Array at Index`"}} javascript/obj_manip -.-> lab-28561{{"`Pull Values From Array at Index`"}} javascript/higher_funcs -.-> lab-28561{{"`Pull Values From Array at Index`"}} end

How to Pull Values From Array at Index

To pull out specific values from an array at certain indexes, 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 store them in a new array called removed.
  3. Set Array.prototype.length to 0 to mutate the original array by resetting its length.
  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.
  6. The function pullAtIndex takes two arguments: the original array and an array of indexes to pull out.
  7. The function returns an array of removed values.

Example usage:

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

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

Summary

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

Other JavaScript Tutorials you may like