Skip to content

mapKeys

Map over the keys of an object

163 bytes
since v12.1.0

Usage

Given an object and a toKey callback function, returns a new object with all the keys mapped through the toKey function. The callback is given both the key and value for each entry.

Only own enumerable properties are visited, including string and symbol keys. Inherited and non-enumerable properties are ignored.

import * as
import _
_
from 'radashi'
const
const ra: {
mode: string;
power: string;
}
ra
= {
mode: string
mode
: 'god',
power: string
power
: 'sun',
}
import _
_
.
function mapKeys<string, "mode" | "power", string>(obj: Record<"mode" | "power", string>, mapFunc: (key: "mode" | "power", value: string) => string): Record<string, string>

Map over the own enumerable string and symbol keys of an object to return a new object.

@see ― https://radashi.js.org/reference/object/mapKeys

@example

const a = { a: 1, b: 2, c: 3 }
mapKeys(a, (key, value) => key + value)
// => { a1: 1, b2: 2, c3: 3 }

@version ― 12.1.0

mapKeys
(
const ra: {
mode: string;
power: string;
}
ra
,
key: "mode" | "power"
key
=>
key: "mode" | "power"
key
.
String.toUpperCase(): string

Converts all the alphabetic characters in a string to uppercase.

toUpperCase
()) // => { MODE, POWER }
import _
_
.
function mapKeys<string, "mode" | "power", string>(obj: Record<"mode" | "power", string>, mapFunc: (key: "mode" | "power", value: string) => string): Record<string, string>

Map over the own enumerable string and symbol keys of an object to return a new object.

@see ― https://radashi.js.org/reference/object/mapKeys

@example

const a = { a: 1, b: 2, c: 3 }
mapKeys(a, (key, value) => key + value)
// => { a1: 1, b2: 2, c3: 3 }

@version ― 12.1.0

mapKeys
(
const ra: {
mode: string;
power: string;
}
ra
, (
key: "mode" | "power"
key
,
value: string
value
) =>
value: string
value
) // => { god: 'god', power: 'power' }

Symbol keys can also be mapped to new symbols:

import * as
import _
_
from 'radashi'
const
const input: typeof input
input
=
var Symbol: SymbolConstructor
(description?: string | number) => symbol

Returns a new unique Symbol value.

@param ― description Description of the new Symbol object.

Symbol
('input')
const
const output: typeof output
output
=
var Symbol: SymbolConstructor
(description?: string | number) => symbol

Returns a new unique Symbol value.

@param ― description Description of the new Symbol object.

Symbol
('output')
import _
_
.
function mapKeys<number, typeof input, typeof output>(obj: Record<typeof input, number>, mapFunc: (key: typeof input, value: number) => typeof output): Record<...>

Map over the own enumerable string and symbol keys of an object to return a new object.

@see ― https://radashi.js.org/reference/object/mapKeys

@example

const a = { a: 1, b: 2, c: 3 }
mapKeys(a, (key, value) => key + value)
// => { a1: 1, b2: 2, c3: 3 }

@version ― 12.1.0

mapKeys
({ [
const input: typeof input
input
]: 22 }, () =>
const output: typeof output
output
) // => { [output]: 22 }

Type-safe alternatives

If your goal is to uppercase or lowercase the keys of an object, Radashi provides purpose-built functions for that, called upperize and lowerize. These functions have better type inference than mapKeys.

import * as _ from 'radashi'
const ra = {
mode: 'god',
power: 'sun',
}
const upperized = _.upperize(ra)
// ^? { MODE: string, POWER: string }
const lowerized = _.lowerize(upperized)
// ^? { mode: string, power: string }