Skip to content

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 { invert } from '@helpers4/object';
invert<K extends string, V extends PropertyKey>(obj: Record<K, V> | null | undefined): Record<V, K>
ParameterTypeDescription
objRecord<K, V> | null | undefinedThe object whose keys and values are to be swapped

Record<V, K> — A new object with values as keys and original keys as values

Returns a new object with keys and values swapped.

invert({ a: 'x', b: 'y', c: 'z' })
// => { x: 'a', y: 'b', z: 'c' }

Useful when you have a code-to-label map and need label-to-code.

const STATUS_LABELS = { 200: 'OK', 404: 'Not Found', 500: 'Internal Server Error' };
const LABEL_TO_CODE = invert(STATUS_LABELS);

LABEL_TO_CODE['OK']; // => '200'

View source on GitHub