diff --git a/README.md b/README.md index 8db504096..33a6f12a1 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,7 @@ average(1, 2, 3); View contents * [`average`](#average) +* [`averageBy`](#averageby) * [`clampNumber`](#clampnumber) * [`digitize`](#digitize) * [`distance`](#distance) @@ -221,7 +222,9 @@ average(1, 2, 3); * [`isPrime`](#isprime) * [`lcm`](#lcm) * [`luhnCheck`](#luhncheck) +* [`maxBy`](#maxby) * [`median`](#median) +* [`minBy`](#minby) * [`percentile`](#percentile) * [`powerset`](#powerset) * [`primes`](#primes) @@ -231,6 +234,7 @@ average(1, 2, 3); * [`sdbm`](#sdbm) * [`standardDeviation`](#standarddeviation) * [`sum`](#sum) +* [`sumBy`](#sumby) * [`sumPower`](#sumpower) * [`toSafeInteger`](#tosafeinteger) @@ -2871,6 +2875,28 @@ average(1, 2, 3); // 2
[⬆ Back to top](#table-of-contents) +### 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; +``` + +
+Examples + +```js +averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### clampNumber Clamps `num` within the inclusive range specified by the boundary values `a` and `b`. @@ -3297,6 +3323,28 @@ luhnCheck(123456789); // false
[⬆ Back to top](#table-of-contents) +### 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)); +``` + +
+Examples + +```js +maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### median Returns the median of an array of numbers. @@ -3324,6 +3372,28 @@ median([5, 6, 50, 1, -5]); // 5
[⬆ Back to top](#table-of-contents) +### 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)); +``` + +
+Examples + +```js +minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### percentile Uses the percentile formula to calculate how many numbers in the given array are less or equal to the given value. @@ -3546,6 +3616,28 @@ sum(...[1, 2, 3, 4]); // 10
[⬆ Back to top](#table-of-contents) +### 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); +``` + +
+Examples + +```js +sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 20 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### sumPower Returns the sum of the powers of all the numbers from `start` to `end` (both inclusive). @@ -5207,6 +5299,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/docs/index.html b/docs/index.html index ef41d8b8a..d4b15929b 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 ]
@@ -625,6 +625,8 @@ document.body📋 Copy to clipboard

Math

average

Returns the average of an of two or more numbers.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;
 
average(...[1, 2, 3]); // 2
 average(1, 2, 3); // 2
+

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.

const averageBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0) / arr.length;
+
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5
 

clampNumber

Clamps num within the inclusive range specified by the boundary values a and b.

If num falls within the range, return num. Otherwise, return the nearest number in the range.

const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
 
clampNumber(2, 3, 5); // 3
 clampNumber(1, -1, -5); // -1
@@ -726,12 +728,16 @@ own individual rating by supplying it as the third argument.
 
luhnCheck('4485275742308327'); // true
 luhnCheck(6011329933655299); //  false
 luhnCheck(123456789); // false
+

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.

const maxBy = (arr, fn) => Math.max(...arr.map(fn));
+
maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8
 

median

Returns the median of an array of numbers.

Find the middle of the array, use Array.sort() to sort the values. Return the number at the midpoint if length is odd, otherwise the average of the two middle numbers.

const median = arr => {
   const mid = Math.floor(arr.length / 2),
     nums = [...arr].sort((a, b) => a - b);
   return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
 };
 
median([5, 6, 50, 1, -5]); // 5
+

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.

const minBy = (arr, fn) => Math.min(...arr.map(fn));
+
minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8
 

percentile

Uses the percentile formula to calculate how many numbers in the given array are less or equal to the given value.

Use Array.reduce() to calculate how many numbers are below the value and how many are the same value and apply the percentile formula.

const percentile = (arr, val) =>
   100 * arr.reduce((acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
 
percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 6); // 55
@@ -771,6 +777,8 @@ own individual rating by supplying it as the third argument.
 standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (population)
 

sum

Returns the sum of two or more numbers/arrays.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
 
sum(...[1, 2, 3, 4]); // 10
+

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.

const sumBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0);
+
sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 20
 

sumPower

Returns the sum of the powers of all the numbers from start to end (both inclusive).

Use Array.fill() to create an array of all the numbers in the target range, Array.map() and the exponent operator (**) to raise them to power and Array.reduce() to add them together. Omit the second argument, power, to use a default power of 2. Omit the third argument, start, to use a default starting value of 1.

const sumPower = (end, power = 2, start = 1) =>
   Array(end + 1 - start)
     .fill(0)
@@ -1166,6 +1174,7 @@ Logs: {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/averageBy.md b/snippets/averageBy.md
index 6f657f8a1..da0995f29 100644
--- a/snippets/averageBy.md
+++ b/snippets/averageBy.md
@@ -5,9 +5,9 @@ Returns the average of an array, after mapping each element to a value using the
 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;
+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
+averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5
 ```
diff --git a/snippets/httpPost.md b/snippets/httpPost.md
index a64c74672..7432560a7 100644
--- a/snippets/httpPost.md
+++ b/snippets/httpPost.md
@@ -28,6 +28,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/maxBy.md b/snippets/maxBy.md
index 2bb7eb190..596a4bd3f 100644
--- a/snippets/maxBy.md
+++ b/snippets/maxBy.md
@@ -9,5 +9,5 @@ const maxBy = (arr, fn) => Math.max(...arr.map(fn));
 ```
 
 ```js
-maxBy([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], o => o.n);  // 8
+maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8
 ```
diff --git a/snippets/minBy.md b/snippets/minBy.md
index 81267b4eb..a185c7fc7 100644
--- a/snippets/minBy.md
+++ b/snippets/minBy.md
@@ -9,5 +9,5 @@ const minBy = (arr, fn) => Math.min(...arr.map(fn));
 ```
 
 ```js
-minBy([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], o => o.n);  // 8
+minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8
 ```
diff --git a/snippets/sumBy.md b/snippets/sumBy.md
index 178e4f0ad..febb9248f 100644
--- a/snippets/sumBy.md
+++ b/snippets/sumBy.md
@@ -9,5 +9,5 @@ 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
+sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 20
 ```