how to use the invertby function from lodash in javascript

The invertBy function from the lodash library creates an object composed of keys generated from the results of running each element of object through iteratee. The corresponding value of each key is an array of the inverted keys responsible for generating the inverted value. The iteratee is invoked with one argument: (value).

Here's an example of using invertBy function:

index.tsx
const _ = require('lodash');

const obj = {
    'a': 1,
    'b': 2,
    'c': 1
};

const result = _.invertBy(obj);

// Output: { '1': [ 'a', 'c' ], '2': [ 'b' ] }
console.log(result);
184 chars
13 lines

In the above example, invertBy function creates an object who's keys are unique values in the original object and values are the original object keys associated with the given value. This function can be useful for grouping objects that share a common property.

gistlibby LogSnag