diff --git a/README.md b/README.md index 10f4d69ca..cfc3c1a51 100644 --- a/README.md +++ b/README.md @@ -1010,7 +1010,7 @@ Use `Array.prototype.reduce()` to create an object, where the keys are produced ```js const countBy = (arr, fn) => - arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => { + arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => { acc[val] = (acc[val] || 0) + 1; return acc; }, {}); diff --git a/docs/index.html b/docs/index.html index fb657e887..5c0caad10 100644 --- a/docs/index.html +++ b/docs/index.html @@ -118,7 +118,7 @@
Removes falsey values from an array.
Use Array.prototype.filter() to filter out falsey values (false, null, 0, "", undefined, and NaN).
const compact = arr => arr.filter(Boolean);
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ]
Groups the elements of an array based on the given function and returns the count of elements in each group.
Use Array.prototype.map() to map the values of an array to a function or property name. Use Array.prototype.reduce() to create an object, where the keys are produced from the mapped results.
const countBy = (arr, fn) => - arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => { + arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => { acc[val] = (acc[val] || 0) + 1; return acc; }, {});