countBy()

Creates or augments an object, making new properties that correspond to values in the given array. Each property added will indicate how many items in the array corresponding to those property names.

This function is valid in v2.12.0 to v2.18.1. This function has been downloaded 17 times.

Parameters

{Array} arr
Array or array-like object containing items to group together and count.
{Function|Array|string} grouper
If a function, it will be passed a value which will be used to group the items in arr. If an array or a string it will be seen as the path within each item in arr to the property by which to group each item.
{Object=} opt_initial Optional, Default:{}
Object to augment with property names that will contain counts of how many items correspond to the values returned by grouper.

Returns

{Object}
opt_initial with property names that will contain counts of how many items correspond to the values returned by grouper.

Required Functions

This function directly requires the following functions which are included automatically:

  • getAt()
    Gets the value under the given root object and path.
  • groupBy()
    Takes an array and groups the items together based on a given hasher function.
  • has()
    Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.
  • indexBy()
    Traverses an array and returns an object with the values from the array placed in the specified indices.
  • typeOf()
    Either gets the type of a value or adds a constructor registering its custom type name.

Requiring Functions

This function is directly required by the following functions:

  • groupBy()
    Takes an array and groups the items together based on a given hasher function.
  • indexBy()
    Traverses an array and returns an object with the values from the array placed in the specified indices.