Add countBy, update other By snippets
Updated all By snippets to have matching behavior, consistent argument naming and added countBy as per #100.
This commit is contained in:
@ -5,9 +5,11 @@ 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.
|
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
|
```js
|
||||||
const averageBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0) / arr.length;
|
const averageBy = (arr, fn) =>
|
||||||
|
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) / arr.length;
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```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
|
||||||
|
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5
|
||||||
```
|
```
|
||||||
|
|||||||
19
snippets/countBy.md
Normal file
19
snippets/countBy.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
### countBy
|
||||||
|
|
||||||
|
Groups the elements of an array based on the given function and returns the count of elements in each group.
|
||||||
|
|
||||||
|
Use `Array.map()` to map the values of an array to a function or property name.
|
||||||
|
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const countBy = (arr, fn) =>
|
||||||
|
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
|
||||||
|
acc[val] = (acc[val] || 0) + 1;
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
countBy([6.1, 4.2, 6.3], Math.floor); // {4: 1, 6: 2}
|
||||||
|
countBy(['one', 'two', 'three'], 'length'); // {3: 2, 5: 1}
|
||||||
|
```
|
||||||
@ -6,8 +6,8 @@ Use `Array.map()` to map the values of an array to a function or property name.
|
|||||||
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
|
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const groupBy = (arr, func) =>
|
const groupBy = (arr, fn) =>
|
||||||
arr.map(typeof func === 'function' ? func : val => val[func]).reduce((acc, val, i) => {
|
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
|
||||||
acc[val] = (acc[val] || []).concat(arr[i]);
|
acc[val] = (acc[val] || []).concat(arr[i]);
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|||||||
@ -5,9 +5,11 @@ Returns the maximum value of an array, after mapping each element to a value usi
|
|||||||
Use `Array.map()` to map each element to the value returned by `fn`, `Math.max()` to get the maximum value.
|
Use `Array.map()` to map each element to the value returned by `fn`, `Math.max()` to get the maximum value.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const maxBy = (arr, fn) => Math.max(...arr.map(fn));
|
const maxBy = (arr, fn) =>
|
||||||
|
Math.max(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```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
|
||||||
|
maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }],'n'); // 8
|
||||||
```
|
```
|
||||||
|
|||||||
@ -5,9 +5,11 @@ Returns the minimum value of an array, after mapping each element to a value usi
|
|||||||
Use `Array.map()` to map each element to the value returned by `fn`, `Math.min()` to get the maximum value.
|
Use `Array.map()` to map each element to the value returned by `fn`, `Math.min()` to get the maximum value.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const minBy = (arr, fn) => Math.min(...arr.map(fn));
|
const minBy = (arr, fn) =>
|
||||||
|
Math.min(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```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
|
||||||
|
minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 8
|
||||||
```
|
```
|
||||||
|
|||||||
@ -5,9 +5,11 @@ Returns the sum of an array, after mapping each element to a value using the pro
|
|||||||
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`.
|
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
|
```js
|
||||||
const sumBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0);
|
const sumBy = (arr, fn) =>
|
||||||
|
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```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
|
||||||
|
sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 20
|
||||||
```
|
```
|
||||||
|
|||||||
@ -18,6 +18,7 @@ collectInto:adapter,function,array
|
|||||||
compact:array
|
compact:array
|
||||||
compose:function
|
compose:function
|
||||||
copyToClipboard:browser,string,advanced
|
copyToClipboard:browser,string,advanced
|
||||||
|
countBy:array,object
|
||||||
countOccurrences:array
|
countOccurrences:array
|
||||||
createElement:browser,utility
|
createElement:browser,utility
|
||||||
createEventHub:browser,event,advanced
|
createEventHub:browser,event,advanced
|
||||||
@ -56,7 +57,7 @@ getScrollPosition:browser
|
|||||||
getStyle:browser,css
|
getStyle:browser,css
|
||||||
getType:type
|
getType:type
|
||||||
getURLParameters:utility,browser,string,url
|
getURLParameters:utility,browser,string,url
|
||||||
groupBy:array
|
groupBy:array,object
|
||||||
hammingDistance:math
|
hammingDistance:math
|
||||||
hasClass:browser,css
|
hasClass:browser,css
|
||||||
hasFlags:node
|
hasFlags:node
|
||||||
|
|||||||
Reference in New Issue
Block a user