DEV Community

Cover image for Invert object with Lodash
Tom Smykowski
Tom Smykowski

Posted on

Invert object with Lodash

Javascript Lodash library helps, among others, to invert an object
Alt Text

Top comments (2)

Collapse
 
alexstaroselsky profile image
Alexander Staroselsky • Edited

Lodash has some interesting features. What do you see as a valid use case for this function? Also here is an oversimplified pure JS version of _.invert:

const invert = (o) =>
  Object.entries(o).reduce(
    (acc, [key, value]) => ({ ...acc, [value]: key }),
    {}
  );

Enter fullscreen mode Exit fullscreen mode
Collapse
 
tomaszs2 profile image
Tom Smykowski

Nice vanilla code,
I use it mostly with data transformations