Modular lodash function that creates an object composed of keys generated from running each element of a collection through an iteratee, with corresponding values representing the count of times each key was returned.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Modular build of lodash's countBy function that creates an object composed of keys generated from running each element of a collection through an iteratee. The corresponding value of each key is the number of times the key was returned by the iteratee.
npm install lodash.countbyvar countBy = require('lodash.countby');var countBy = require('lodash.countby');
// Basic function iteratee
var result1 = countBy([4.3, 6.1, 6.4], function(n) {
return Math.floor(n);
});
// => { '4': 1, '6': 2 }
// Property name as iteratee
var result2 = countBy(['one', 'two', 'three'], 'length');
// => { '3': 2, '5': 1 }
// Array of objects
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'betty', 'active': true },
{ 'user': 'fred', 'active': false }
];
var result3 = countBy(users, 'active');
// => { 'true': 2, 'false': 1 }Creates an object composed of keys generated from the results of running each element of a collection through an iteratee function. The corresponding value represents the count of elements that generated each key.
/**
* Creates an object composed of keys generated from running each element
* of collection through iteratee. The corresponding value of each key is
* the number of times the key was returned by iteratee.
*
* @param {Array|Object|string} collection - The collection to iterate over
* @param {Function|Object|string} [iteratee=_.identity] - The function invoked per iteration
* @param {*} [thisArg] - The `this` binding of iteratee
* @returns {Object} Returns the composed aggregate object
*/
function countBy(collection, iteratee, thisArg);Array|Object|string): The collection to iterate over. Can be an array, object, or string.Function|Object|string, optional): The function invoked per iteration. Defaults to identity function if not provided.
(value, index|key, collection) => any - return value becomes the grouping key_.matchesProperty style callback that returns true/false based on whether the property value matches thisArgtrue/false based on whether elements match the provided object properties*, optional): The this binding of iteratee when iteratee is a function, or the value to match against when iteratee is a property namevar countBy = require('lodash.countby');
// Function iteratee with Math context
var numbers = [4.3, 6.1, 6.4];
var floored = countBy(numbers, function(n) {
return this.floor(n);
}, Math);
// => { '4': 1, '6': 2 }
// Property name iteratee
var words = ['one', 'two', 'three'];
var byLength = countBy(words, 'length');
// => { '3': 2, '5': 1 }
// Property name + thisArg (matchesProperty pattern)
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'betty', 'age': 24 },
{ 'user': 'fred', 'age': 36 }
];
var ageMatches = countBy(users, 'age', 36);
// => { 'false': 2, 'true': 1 } (counts based on age === 36 match result)
// Object matcher iteratee
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'betty', 'age': 24, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true }
];
var activeCount = countBy(users, { 'active': true });
// => { 'false': 1, 'true': 2 } (counts based on match result)The function supports various collection types:
// Array collection
countBy([1, 2, 1, 3, 2], function(n) { return n; });
// => { '1': 2, '2': 2, '3': 1 }
// Object collection
countBy({ 'a': 1, 'b': 2, 'c': 1 }, function(value) { return value; });
// => { '1': 2, '2': 1 }
// String collection
countBy('hello', function(char) { return char; });
// => { 'h': 1, 'e': 1, 'l': 2, 'o': 1 }