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.14.0. This function has been downloaded 33 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 requires the following functions which are included automatically:

  • 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.
  • getAt()
    Gets the value under the given root object and path.
  • typeOf()
    Either gets the type of a value or adds a constructor registering its custom type name.
  • 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.