or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-countby

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.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.countby@3.1.x

To install, run

npx @tessl/cli install tessl/npm-lodash-countby@3.1.0

index.mddocs/

lodash.countby

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.

Package Information

  • Package Name: lodash.countby
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.countby

Core Imports

var countBy = require('lodash.countby');

Basic Usage

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 }

Capabilities

Collection Counting

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);

Parameters

  • collection (Array|Object|string): The collection to iterate over. Can be an array, object, or string.
  • iteratee (Function|Object|string, optional): The function invoked per iteration. Defaults to identity function if not provided.
    • Function: Custom function (value, index|key, collection) => any - return value becomes the grouping key
    • Property name string: Creates a property accessor that returns the specified property value as the key
    • Property name + thisArg: When both property name and thisArg are provided, creates a _.matchesProperty style callback that returns true/false based on whether the property value matches thisArg
    • Object: Creates a matcher that returns true/false based on whether elements match the provided object properties
  • thisArg (*, optional): The this binding of iteratee when iteratee is a function, or the value to match against when iteratee is a property name

Return Value

  • Object: An object where keys are the values returned by the iteratee and values are the count of elements that generated each key

Iteratee Behavior Examples

var 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)

Collection Type Support

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 }