The object to iterate over.
If provided, it is used as the initial value to start the accumulation. Otherwise, an empty object {} will be used.
The function to call for each key where the first argument is the previous value of the accumulated result.
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.
The object to iterate over.
If provided, it is used as the initial value to start the accumulation. Otherwise, an empty object {} will be used.
The function to call for each key where the first argument is the previous value of the accumulated result.
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.
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.