Skip to content

invert

Invert the keys and values of an object

94 bytes

Usage

Given an object returns a new object with the keys and values reversed.

import * as
import _
_
from 'radashi'
const
const powersByGod: {
ra: string;
loki: string;
zeus: string;
}
powersByGod
= {
ra: string
ra
: 'sun',
loki: string
loki
: 'tricks',
zeus: string
zeus
: 'lightning',
}
import _
_
.
invert<"ra" | "loki" | "zeus", string>(obj: Record<"ra" | "loki" | "zeus", string>): Record<string, "ra" | "loki" | "zeus">
export invert

Returns a new object whose keys are the values of the given object and its values are the keys of the given object.

@seehttps://radashi.js.org/reference/object/invert

@example

const a = { a: 1, b: 2, c: 3 }
invert(a)
// => { 1: 'a', 2: 'b', 3: 'c' }

@version12.1.0

invert
(
const powersByGod: {
ra: string;
loki: string;
zeus: string;
}
powersByGod
) // => { sun: ra, tricks: loki, lightning: zeus }