Introduction
In this lab, we will be exploring the pullBy() function in JavaScript. This function allows us to filter out specific values from an array based on a given iterator function. By the end of this lab, you will understand how to use pullBy() to manipulate arrays and effectively filter out unwanted values.
How to Pull Values from an Array based on a Given Function
To start practicing coding, open the Terminal/SSH and type node.
The function pullBy mutates the original array by filtering out the specified values based on a given iterator function. Here's how it works:
- Check if the last argument provided is a function.
- Use
Array.prototype.map()to apply the iterator functionfnto all array elements. - Use
Array.prototype.filter()andArray.prototype.includes()to pull out the values that are not needed. - Set
Array.prototype.lengthto reset the passed-in array's length to0. - Use
Array.prototype.push()to re-populate it with only the pulled values.
Here's the code:
const pullBy = (arr, ...args) => {
const length = args.length;
let fn = length > 1 ? args[length - 1] : undefined;
fn = typeof fn == "function" ? (args.pop(), fn) : undefined;
let argState = (Array.isArray(args[0]) ? args[0] : args).map((val) =>
fn(val)
);
let pulled = arr.filter((v, i) => !argState.includes(fn(v)));
arr.length = 0;
pulled.forEach((v) => arr.push(v));
};
And here's an example of how to use it:
var myArray = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }];
pullBy(myArray, [{ x: 1 }, { x: 3 }], (o) => o.x); // myArray = [{ x: 2 }]
Note that in this example, we're pulling out all elements with an x property of 1 or 3. The resulting myArray will only contain the element with an x property of 2.
Summary
Congratulations! You have completed the Pull Values From Array Based on Function lab. You can practice more labs in LabEx to improve your skills.