Function Namespace Pot.reduce
Defined in: <pot.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
Pot.reduce(object, callback, initial, (context))
Apply a function against an accumulator and each value of
the object (from left-to-right) as to reduce it to a single value.
|
Function Namespace Detail
Pot.reduce(object, callback, initial, (context))
Apply a function against an accumulator and each value of
the object (from left-to-right) as to reduce it to a single value.
This method like Array.prototype.reduce
var array = [1, 2, 3, 4, 5]; var total = Pot.reduce(array, function(a, b) { return a + b; }); debug(total); // @results 15
var object = {a: 1, b: 2, c: 3}; var total = Pot.reduce(object, function(a, b) { return a + b; }); debug(total); // @results 6
- Parameters:
- {Array|Object|*} object
- A target object.
- {Function} callback
- A callback function.
- {*} initial
- An initial value passed as `callback` argument that will be used on first iteration.
- {*} (context)
- (Optional) Object to use as the first argument to the first call of the `callback`.
- Returns:
- {*} Return the result of each callbacks.