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'.