mapKeys
Map over the keys of an object
100 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.
import * as _ from 'radashi'
const ra = { mode: 'god', power: 'sun',}
_.mapKeys(ra, key => key.toUpperCase()) // => { MODE, POWER }_.mapKeys(ra, (key, value) => value) // => { god: 'god', power: 'power' }
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 }