Loops
/ API ReferenceLoops
/ API ReferenceobjectReduce
Reduces the object based on the provided reducer function.
Similar to Array.prototype.reduce
, but for objects.
The Gist
import { objectReduce } from '@weser/loops'
const reducedObject = objectReduce(
{ a: 1, b: 2, c: 3 },
(accumulator, value) => accumulator + value,
0
)
// => 6
console.log(reducedObject)
Parameters
Parameter | Type | Description |
---|---|---|
obj | Record<PropertyKey, any> | The object to reduce. |
reducer | (accumulator: B, value: T[keyof T], key: keyof T, obj: T) => B | The reducer function. |
initialValue | B | The initial value. |
Returns
(B
) The reduced value.
© 2024-present Robin Weser. All Rights Reserved.