diff --git a/snippets/averageBy.md b/snippets/averageBy.md new file mode 100644 index 000000000..6f657f8a1 --- /dev/null +++ b/snippets/averageBy.md @@ -0,0 +1,13 @@ +### averageBy + +Returns the average of an array, after mapping each element to a value using the provided function. + +Use `Array.map()` to map each element to the value returned by `fn`, `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array. + +```js +const averageBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0)/arr.length; +``` + +```js +averageBy([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], o => o.n); // 5 +``` diff --git a/snippets/maxBy.md b/snippets/maxBy.md new file mode 100644 index 000000000..2bb7eb190 --- /dev/null +++ b/snippets/maxBy.md @@ -0,0 +1,13 @@ +### maxBy + +Returns the maximum value of an array, after mapping each element to a value using the provided function. + +Use `Array.map()` to map each element to the value returned by `fn`, `Math.max()` to get the maximum value. + +```js +const maxBy = (arr, fn) => Math.max(...arr.map(fn)); +``` + +```js +maxBy([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], o => o.n); // 8 +``` diff --git a/snippets/minBy.md b/snippets/minBy.md new file mode 100644 index 000000000..81267b4eb --- /dev/null +++ b/snippets/minBy.md @@ -0,0 +1,13 @@ +### minBy + +Returns the minimum value of an array, after mapping each element to a value using the provided function. + +Use `Array.map()` to map each element to the value returned by `fn`, `Math.min()` to get the maximum value. + +```js +const minBy = (arr, fn) => Math.min(...arr.map(fn)); +``` + +```js +minBy([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], o => o.n); // 8 +``` diff --git a/snippets/sumBy.md b/snippets/sumBy.md new file mode 100644 index 000000000..178e4f0ad --- /dev/null +++ b/snippets/sumBy.md @@ -0,0 +1,13 @@ +### sumBy + +Returns the sum of an array, after mapping each element to a value using the provided function. + +Use `Array.map()` to map each element to the value returned by `fn`, `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`. + +```js +const sumBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0); +``` + +```js +sumBy([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], o => o.n); // 20 +``` diff --git a/tag_database b/tag_database index ee007d575..ce9bf0ad2 100644 --- a/tag_database +++ b/tag_database @@ -1,6 +1,7 @@ anagrams:string,recursion arrayToHtmlList:browser,array average:math,array +averageBy:math,array,function bottomVisible:browser byteSize:string call:adapter,function @@ -102,9 +103,11 @@ lowercaseKeys:object luhnCheck:math,utility mapObject:array,object mask:string,utility,regexp +maxBy:math,array,function maxN:array,math median:math,array memoize:function +minBy:math,array,function minN:array,amth negate:function nthElement:array @@ -158,6 +161,7 @@ splitLines:string spreadOver:adapter standardDeviation:math,array sum:math,array +sumBy:math,array,function sumPower:math symmetricDifference:array,math tail:array