Skip to content

invert

Creates a new object by swapping the keys and values of the given object.

This function takes an object and creates a new object where the keys are the values and the values are the keys of the original object. If there are duplicate values in the input object, the key that appears last will be used as the new key.

Signature

typescript
function invert<K extends string | number | symbol, V extends string | number | symbol>(
  obj: Record<K, V>
): { [key in V]: K };

Parameters

  • obj (Record<K, V>): The object to invert.

Returns

({ [key in V]: K }): A new object with keys and values inverted.

Examples

typescript
const obj = { a: 1, b: 1, c: 2 };
const result = invert(obj);
// result will be { 1: 'b', 2: 'c' }

Released under the MIT License.