diff --git a/snippets/averageBy.md b/snippets/averageBy.md index da0995f29..e31117182 100644 --- a/snippets/averageBy.md +++ b/snippets/averageBy.md @@ -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. ```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 averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5 +averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5 ``` diff --git a/snippets/countBy.md b/snippets/countBy.md new file mode 100644 index 000000000..8001aff25 --- /dev/null +++ b/snippets/countBy.md @@ -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} +``` diff --git a/snippets/groupBy.md b/snippets/groupBy.md index 5d7f5bc55..e23c5c996 100644 --- a/snippets/groupBy.md +++ b/snippets/groupBy.md @@ -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. ```js -const groupBy = (arr, func) => - arr.map(typeof func === 'function' ? func : val => val[func]).reduce((acc, val, i) => { +const groupBy = (arr, fn) => + arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => { acc[val] = (acc[val] || []).concat(arr[i]); return acc; }, {}); diff --git a/snippets/maxBy.md b/snippets/maxBy.md index 596a4bd3f..9186dd009 100644 --- a/snippets/maxBy.md +++ b/snippets/maxBy.md @@ -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. ```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 maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8 +maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }],'n'); // 8 ``` diff --git a/snippets/minBy.md b/snippets/minBy.md index a185c7fc7..9f735119c 100644 --- a/snippets/minBy.md +++ b/snippets/minBy.md @@ -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. ```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 minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8 +minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 8 ``` diff --git a/snippets/sumBy.md b/snippets/sumBy.md index febb9248f..6e5812cc3 100644 --- a/snippets/sumBy.md +++ b/snippets/sumBy.md @@ -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`. ```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 sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 20 +sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 20 ``` diff --git a/tag_database b/tag_database index 9becb5b1a..45bce9b45 100644 --- a/tag_database +++ b/tag_database @@ -18,6 +18,7 @@ collectInto:adapter,function,array compact:array compose:function copyToClipboard:browser,string,advanced +countBy:array,object countOccurrences:array createElement:browser,utility createEventHub:browser,event,advanced @@ -56,7 +57,7 @@ getScrollPosition:browser getStyle:browser,css getType:type getURLParameters:utility,browser,string,url -groupBy:array +groupBy:array,object hammingDistance:math hasClass:browser,css hasFlags:node