A new object with the same keys but with values transformed by the function.
This utility is useful for transforming the values of an object without modifying the original object. It iterates over each key-value pair in the object and applies the provided function to generate a new value.
// ES Module
import { mapValues } from '@bnidev/js-utils'
// CommonJS
const { mapValues } = require('@bnidev/js-utils')
// Map the values of an object to their lengths
const obj = { a: 'apple', b: 'banana', c: 'cherry' }
const lengths = mapValues(obj, (value, key) => value.length)
// lengths will be { a: 5, b: 6, c: 6 }
// Map the values of an object to uppercase strings
const mixedObj = { x: 'hello', y: 'world' }
const uppercased = mapValues(mixedObj, (value, key) => value.toUpperCase())
// uppercased will be { x: 'HELLO', y: 'WORLD' }
Maps the values of an object to a new value based on a provided function.