Function mapKeys

  • Creates a new object with the same values as the given object, but with keys generated by running each own enumerable property of the object through the iteratee function.

    Type Parameters

    • T extends Record<PropertyKey, any>

      The type of the object.

    • K extends PropertyKey

      The type of the new keys generated by the iteratee function.

    Parameters

    • object: T

      The object to iterate over.

    • getNewKey: ((value: T[keyof T], key: keyof T, object: T) => K)

      The function invoked per own enumerable property.

        • (value, key, object): K
        • Parameters

          • value: T[keyof T]
          • key: keyof T
          • object: T

          Returns K

    Returns Record<K, T[keyof T]>

    • Returns the new mapped object.
    // Example usage:
    const obj = { a: 1, b: 2 };
    const result = mapKeys(obj, (value, key) => key + value);
    console.log(result); // { a1: 1, b2: 2 }