Introduction
In this lab, we will explore the implementation of the differenceBy function in JavaScript. This function allows us to find the difference between two arrays by applying a provided function to each element in both arrays. We will learn how to use Set, Array.prototype.map(), and Array.prototype.filter() to effectively compare and filter arrays based on a specific criterion.
Function to Return the Difference of Two Arrays by Mapping
To get started with coding, open your Terminal/SSH and type node.
This function takes two arrays and applies the provided function to each element in both arrays to return their difference.
To do this:
- Create a
Setby applying the function (fn) to each element in the second array (b). - Use
Array.prototype.map()to apply the function (fn) to each element in the first array (a). - Use
Array.prototype.filter()in combination with the function (fn) on the first array (a) to only keep values not contained in the second array (b), usingSet.prototype.has().
Here's the code for the function:
const differenceBy = (a, b, fn) => {
const s = new Set(b.map(fn));
return a.map(fn).filter((el) => !s.has(el));
};
Here are some examples of how to use the function:
differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1]
differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], (v) => v.x); // [2]
Summary
Congratulations! You have completed the Mapped Array Difference lab. You can practice more labs in LabEx to improve your skills.