diff --git a/README.md b/README.md index e15f1990a..8067f8213 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ average(1, 2, 3); * [`pullAtValue`](#pullatvalue) * [`reducedFilter`](#reducedfilter) * [`reduceSuccessive`](#reducesuccessive) +* [`reduceWhich`](#reducewhich) * [`remove`](#remove) * [`sample`](#sample) * [`sampleSize`](#samplesize) @@ -412,15 +413,6 @@ average(1, 2, 3); -### _Uncategorized_ - -
-View contents - -* [`reduceWhich`](#reducewhich) - -
- --- ## 🔌 Adapter @@ -1726,6 +1718,35 @@ reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6
[⬆ Back to top](#table-of-contents) +### reduceWhich + +Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule. + +Use `Array.reduce()` in combination with the `comparator` function to get the appropriate element in the array. +You can omit the second parameter, `comparator`, to use the default one that returns the minimum element in the array. + +```js +const reduceWhich = (arr, comparator = (a, b) => a - b) => + arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a)); +``` + +
+Examples + +```js +reduceWhich([1, 3, 2]); // 1 +reduceWhich([1, 3, 2], (a, b) => b - a); // 3 +reduceWhich( + [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }], + (a, b) => a.age - b.age +); // {name: "Lucy", age: 9} +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### remove Removes elements from an array for which the given function returns `false`. @@ -7336,32 +7357,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _Uncategorized_ - -### reduceWhich - -Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule. - -Use `Array.reduce()` in combination with the `comparator` function to get the appropriate element in the array. -You can omit the second parameter, `comparator`, to use the default one that returns the minimum element in the array. - -```js -const reduceWhich = (arr, comparator = (a, b) => a - b) => - arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a)); -``` - -```js -reduceWhich([1, 3, 2]); // 1 -reduceWhich([1, 3, 2], (a, b) => b - a); // 3 -reduceWhich( - [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }], - (a, b) => a.age - b.age -); // {name: "Lucy", age: 9} -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index db73c3d82..a95790372 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

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

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

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

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);
@@ -311,6 +311,14 @@ Object.assig
 

reduceSuccessive

Applies a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values.

Use Array.reduce() to apply the given function to the given array, storing each new result.

const reduceSuccessive = (arr, fn, acc) =>
   arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res), [acc]);
 
reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0); // [0, 1, 3, 6, 10, 15, 21]
+

reduceWhich

Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule.

Use Array.reduce() in combination with the comparator function to get the appropriate element in the array. You can omit the second parameter, comparator, to use the default one that returns the minimum element in the array.

const reduceWhich = (arr, comparator = (a, b) => a - b) =>
+  arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a));
+
reduceWhich([1, 3, 2]); // 1
+reduceWhich([1, 3, 2], (a, b) => b - a); // 3
+reduceWhich(
+  [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }],
+  (a, b) => a.age - b.age
+); // {name: "Lucy", age: 9}
 

remove

Removes elements from an array for which the given function returns false.

Use Array.filter() to find array elements that return truthy values and Array.reduce() to remove elements using Array.splice(). The func is invoked with three arguments (value, index, array).

const remove = (arr, func) =>
   Array.isArray(arr)
     ? arr.filter(func).reduce((acc, val) => {
@@ -1706,12 +1714,4 @@ Logs: {
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

reduceWhich

Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule.

Use Array.reduce() in combination with the comparator function to get the appropriate element in the array. You can omit the second parameter, comparator, to use the default one that returns the minimum element in the array.

const reduceWhich = (arr, comparator = (a, b) => a - b) =>
-  arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a));
-
reduceWhich([1, 3, 2]); // 1
-reduceWhich([1, 3, 2], (a, b) => b - a); // 3
-reduceWhich(
-  [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }],
-  (a, b) => a.age - b.age
-); // {name: "Lucy", age: 9}
-
\ No newline at end of file +
\ No newline at end of file