uft
    Preparing search index...

    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.

      Returns T

      The final accumulated result.

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

      // An empty object as the initial 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 initial 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
      // A custom return type can be specified in two ways.
      // The second method will only work with an object type
      // where the initial value is an empty object ({}).

      // 1. Specify a custom type using `as` on the initial 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 initial 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
      }
      )
    • Calls a function for each key in an object and returns the accumulated result.

      Type Parameters

      • T extends object
      • U

      Parameters

      • obj: T

        The object to iterate over.

      • initialValue: U

        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: NoInfer<U>,
            key: K,
            currentIndex: number,
            obj: T,
        ) => NoInfer<U>

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

      Returns NoInfer<U>

      The final accumulated result.

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

      // An empty object as the initial 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 initial 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
      // A custom return type can be specified in two ways.
      // The second method will only work with an object type
      // where the initial value is an empty object ({}).

      // 1. Specify a custom type using `as` on the initial 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 initial 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
      }
      )
    • Calls a function for each key in an object and returns the accumulated result.

      Type Parameters

      • T extends object
      • U extends object

      Parameters

      • obj: T

        The object to iterate over.

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

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

      Returns NoInfer<U>

      The final accumulated result.

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

      // An empty object as the initial 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 initial 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
      // A custom return type can be specified in two ways.
      // The second method will only work with an object type
      // where the initial value is an empty object ({}).

      // 1. Specify a custom type using `as` on the initial 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 initial 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
      }
      )