invert
Returns a new object with keys and values swapped.
If multiple keys share the same value, the last one wins.
null and undefined are treated as empty objects and return {}.
Entries whose source key or value is a prototype-polluting string
(__proto__, constructor, prototype) are silently skipped.
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
obj | Record<K, V> | null | undefined | The object whose keys and values are to be swapped |
Returns
Section titled “Returns”Record<V, K> — A new object with values as keys and original keys as values
Examples
Section titled “Examples”Swap keys and values
Section titled “Swap keys and values”Returns a new object with keys and values swapped.
Build a reverse lookup map
Section titled “Build a reverse lookup map”Useful when you have a code-to-label map and need label-to-code.
