@bnidev/js-utils
    Preparing search index...

    Function mapValues

    • Maps the values of an object to a new value based on a provided function.

      Type Parameters

      • T extends object
      • U

      Parameters

      • obj: T

        The object whose values are to be mapped.

      • fn: (value: T[keyof T], key: keyof T) => U

        A function that takes a value and its key, and returns a new value.

      Returns { [K in string | number | symbol]: U }

      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' }