_.invertBy

_.invertBy(object, [iteratee=_.identity])

source npm package

This method is like _.invert except that the inverted object is generated from the results of running each element of object thru iteratee. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. The iteratee is invoked with one argument: (value).

Since

4.1.0

Arguments

  1. object (Object): The object to invert.
  2. [iteratee=_.identity] (Function): The iteratee invoked per element.

Returns

(Object): Returns the new inverted object.

Example

var object = { 'a': 1, 'b': 2, 'c': 1 };
 
_.invertBy(object);
// => { '1': ['a', 'c'], '2': ['b'] }
 
_.invertBy(object, function(value) {
  return 'group' + value;
});
// => { 'group1': ['a', 'c'], 'group2': ['b'] }