Underscore is a library of functions for dealing with complex data structures in JavaScript. One of its most useful functions is the invert
function, which swaps the keys and values of an object.
The basic syntax for _.invert(obj)
is:
index.tsx14 chars2 lines
Here's an example of how to use _.invert(obj)
:
index.tsx182 chars12 lines
This will output:
index.tsx60 chars6 lines
Here, _.invert(obj)
takes the obj
object and swaps the keys and values, returning the invertedObj
object.
gistlibby LogSnag