DEV Community

loading...
Cover image for Invert object with Lodash

Invert object with Lodash

tomaszs2 profile image Tomasz Smykowski ・1 min read

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

Discussion (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
Tomasz Smykowski Author

Nice vanilla code,
I use it mostly with data transformations

Forem Open with the Forem app