Function reduceKeys

  • Calls a function for each key in an object and returns the accumulated result.

    Type Parameters

    • T extends object

    Parameters

    • obj: T

      The object to iterate over.

    • initialValue: EmptyObject | NoInfer<T>

      If provided, it is used as the initial value to start the accumulation. Otherwise, an empty object {} will be used.

    • callbackfn: (<K extends string | number | symbol>(previousValue: T, key: K, currentIndex: number, obj: T) => T)

      The function to call for each key where the first argument is the previous value of the accumulated result.

        • <K extends string | number | symbol>(previousValue: T, key: K, currentIndex: number, obj: T): T
        • Type Parameters

          • K extends string | number | symbol

          Parameters

          • previousValue: T
          • key: K
          • currentIndex: number
          • obj: T

          Returns T

    Returns T

    The final accumulated result.

    Remarks

    Keys are the object's own enumerable string-keyed property names, the same as those returned by Object.keys().

    Example

    Basic Usage

    // An empty object as the inital value:
    const objectAccum = reduceKeys(
    { a: 'hello', b: 'world' },
    {},
    (accum, key, index, obj) => {
    accum // type: { a: string, b: string }
    accum[key] = obj[key].toUpperCase()
    return accum
    }
    )
    console.log(objectAccum) // { a: 'HELLO', b: 'WORLD' }

    // The inital value does not need to be an object.
    // It can be any kind, for example a number:
    const numberAccum = reduceKeys(
    { a: 1, b: 2, c: 3 },
    0,
    (accum, key, index, obj) => {
    accum // type: number
    return accum + obj[key]
    }
    )
    console.log(numberAccum) // 6

    Custom return type

    // A custom return type can be specified in two ways.
    // The second method will only work with an object type
    // where the inital value is an empty object ({}).

    // 1. Specify a custom type using `as` on the inital value:
    const strToNumAccum = reduceKeys(
    { a: '1', b: '2' },
    {} as { a: number; b: number },
    (accum, key, index, obj) => {
    accum // type: { a: number; b: number }
    obj // type: { a: string; b: string }

    const strValue = obj[key]
    accum[key] = parseInt(strValue) * 10
    return accum
    }
    )
    console.log(strToNumAccum) // { a: 10, b: 20 }

    // 2. If the inital value is an empty object as shown above,
    // you can omit it and specify the return type by overriding
    // `previousValue` (accum) in the callback:
    const strToNumAccum = reduceKeys(
    { a: '1', b: '2' },
    (accum: { a: number; b: number }, key, index, obj) => {
    obj // type: { a: string; b: string }
    // ... same as above
    }
    )
  • Type Parameters

    • T extends object

    • U

    Parameters

    • obj: T
    • initialValue: U
    • callbackfn: (<K extends string | number | symbol>(previousValue: NoInfer<U>, key: K, currentIndex: number, obj: T) => NoInfer<U>)
        • <K extends string | number | symbol>(previousValue: NoInfer<U>, key: K, currentIndex: number, obj: T): NoInfer<U>
        • Type Parameters

          • K extends string | number | symbol

          Parameters

          • previousValue: NoInfer<U>
          • key: K
          • currentIndex: number
          • obj: T

          Returns NoInfer<U>

    Returns NoInfer<U>

  • Type Parameters

    • T extends object

    • U extends object

    Parameters

    • obj: T
    • callbackfn: (<K extends string | number | symbol>(previousValue: U, key: K, currentIndex: number, obj: T) => NoInfer<U>)
        • <K extends string | number | symbol>(previousValue: U, key: K, currentIndex: number, obj: T): NoInfer<U>
        • Type Parameters

          • K extends string | number | symbol

          Parameters

          • previousValue: U
          • key: K
          • currentIndex: number
          • obj: T

          Returns NoInfer<U>

    Returns NoInfer<U>

Generated using TypeDoc