diff --git a/README.md b/README.md index ab8d611dd..1c9c073c1 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,8 @@ average(1, 2, 3); * [`take`](#take) * [`takeRight`](#takeright) * [`union`](#union) +* [`unionBy`](#unionby) +* [`unionWith`](#unionwith) * [`uniqueElements`](#uniqueelements) * [`without`](#without) * [`zip`](#zip) @@ -1892,6 +1894,56 @@ union([1, 2, 3], [4, 3, 2]); // [1,2,3,4]
[⬆ Back to top](#table-of-contents) +### unionBy + +Returns every element that exists in any of the two arrays once, after applying the provided function to each array element of both. + +Create a `Set` by applying all `fn` to all values of `a`. +Create a `Set` from `a` and all elements in `b` whose value, after applying `fn` does not match a value in the previously created set. +Return the last set converted to an array. + +```js +const unionBy = (a, b, fn) => { + const s = new Set(a.map(v => fn(v))); + return Array.from(new Set([...a, ...b.filter(x => !s.has(fn(x)))])); +}; +``` + +
+Examples + +```js +unionBy([2.1], [1.2, 2.3], Math.floor); // [2.1, 1.2] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### unionWith + +Returns every element that exists in any of the two arrays once, using a provided comparator function. + +Create a `Set` with all values of `a` and values in `b` for which the comparator finds no matches in `a`, using `Array.findIndex()`. + +```js +const unionWith = (a, b, comp) => + Array.from(new Set([...a, ...b.filter(x => a.findIndex(y => comp(x, y)) === -1)])); +``` + +
+Examples + +```js +unionWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0, 3.9], (a, b) => Math.round(a) === Math.round(b)); // [1, 1.2, 1.5, 3, 0, 3.9] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### uniqueElements Returns all unique values of an array. diff --git a/docs/index.html b/docs/index.html index 815e5ed87..e54e5312d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -361,6 +361,14 @@ Object.assig
 takeRight([1, 2, 3]); // [3]
 

union

Returns every element that exists in any of the two arrays once.

Create a Set with all values of a and b and convert to an array.

const union = (a, b) => Array.from(new Set([...a, ...b]));
 
union([1, 2, 3], [4, 3, 2]); // [1,2,3,4]
+

unionBy

Returns every element that exists in any of the two arrays once, after applying the provided function to each array element of both.

Create a Set by applying all fn to all values of a. Create a Set from a and all elements in b whose value, after applying fn does not match a value in the previously created set. Return the last set converted to an array.

const unionBy = (a, b, fn) => {
+  const s = new Set(a.map(v => fn(v)));
+  return Array.from(new Set([...a, ...b.filter(x => !s.has(fn(x)))]));
+};
+
unionBy([2.1], [1.2, 2.3], Math.floor); // [2.1, 1.2]
+

unionWith

Returns every element that exists in any of the two arrays once, using a provided comparator function.

Create a Set with all values of a and values in b for which the comparator finds no matches in a, using Array.findIndex().

const unionWith = (a, b, comp) =>
+  Array.from(new Set([...a, ...b.filter(x => a.findIndex(y => comp(x, y)) === -1)]));
+
unionWith([1, 1.2, 1.5, 3, 0], [1.9, 3, 0, 3.9], (a, b) => Math.round(a) === Math.round(b)); // [1, 1.2, 1.5, 3, 0, 3.9]
 

uniqueElements

Returns all unique values of an array.

Use ES6 Set and the ...rest operator to discard all duplicated values.

const uniqueElements = arr => [...new Set(arr)];
 
uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1,2,3,4,5]
 

without

Filters out the elements of an array, that have one of the specified values.

Use Array.filter() to create an array excluding(using !Array.includes()) all given values.

(For a snippet that mutates the original array see pull)

const without = (arr, ...args) => arr.filter(v => !args.includes(v));
diff --git a/snippets/unionWith.md b/snippets/unionWith.md
index dbfee938b..0243a12aa 100644
--- a/snippets/unionWith.md
+++ b/snippets/unionWith.md
@@ -6,9 +6,7 @@ Create a `Set` with all values of `a` and values in `b` for which the comparator
 
 ```js
 const unionWith = (a, b, comp) =>
-  Array.from(
-    new Set([...a, ...b.filter(x => a.findIndex(y => comp(x, y)) === -1)])
-  );
+  Array.from(new Set([...a, ...b.filter(x => a.findIndex(y => comp(x, y)) === -1)]));
 ```
 
 ```js