diff --git a/README.md b/README.md index 766363348..3adae4b9e 100644 --- a/README.md +++ b/README.md @@ -1684,7 +1684,7 @@ Create a `Set` by applying `fn` to all elements in `b`, then use `Array.filter() ```js const intersectionBy = (a, b, fn) => { - const s = new Set(b.map(x => fn(x))); + const s = new Set(b.map(fn)); return a.filter(x => s.has(fn(x))); }; ``` diff --git a/docs/array.html b/docs/array.html index 59167a256..2352cab74 100644 --- a/docs/array.html +++ b/docs/array.html @@ -224,7 +224,7 @@ };
intersection([1, 2, 3], [4, 3, 2]); // [2,3]
 

intersectionBy

Returns a list of elements that exist in both arrays, after applying the provided function to each array element of both.

Create a Set by applying fn to all elements in b, then use Array.filter() on a to only keep elements, which produce values contained in b when fn is applied to them.

const intersectionBy = (a, b, fn) => {
-  const s = new Set(b.map(x => fn(x)));
+  const s = new Set(b.map(fn));
   return a.filter(x => s.has(fn(x)));
 };
 
intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [2.1]