mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Consolidate aggregate modules.
This commit is contained in:
19
countBy.js
19
countBy.js
@@ -1,5 +1,5 @@
|
||||
import baseAssignValue from './.internal/baseAssignValue.js';
|
||||
import createAggregator from './.internal/createAggregator.js';
|
||||
import reduce from './reduce.js';
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
@@ -20,12 +20,15 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
* countBy([6.1, 4.2, 6.3], Math.floor);
|
||||
* // => { '4': 1, '6': 2 }
|
||||
*/
|
||||
const countBy = createAggregator((result, value, key) => {
|
||||
if (hasOwnProperty.call(result, key)) {
|
||||
++result[key];
|
||||
} else {
|
||||
baseAssignValue(result, key, 1);
|
||||
}
|
||||
});
|
||||
function countBy(collection, iteratee) {
|
||||
return reduce(collection, (result, value, key) => {
|
||||
if (hasOwnProperty.call(result, key)) {
|
||||
++result[key];
|
||||
} else {
|
||||
baseAssignValue(result, key, 1);
|
||||
}
|
||||
return result;
|
||||
}, {});
|
||||
}
|
||||
|
||||
export default countBy;
|
||||
|
||||
Reference in New Issue
Block a user