Simplify countBy
This commit is contained in:
@ -7,7 +7,7 @@ Use `Array.prototype.reduce()` to create an object, where the keys are produced
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const countBy = (arr, fn) =>
|
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;
|
acc[val] = (acc[val] || 0) + 1;
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
const countBy = (arr, fn) =>
|
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;
|
acc[val] = (acc[val] || 0) + 1;
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
|
|||||||
Reference in New Issue
Block a user