back to Overview

.reduce()

Reduces the list into a single value with the help of a callback function.
Util module only.

Syntax

list.reduce(callback, memo)

Parameters

callback
The callback function(memo, item, index) to invoke for each list element.
memo
On the first invocation, the memo argument given to reduce(). On all further invocation, this is the return value of the previous invocation.
item
The current list element.
index
The second the zero-based index of the current element.
this
This list.
(callback return value)
Will be used as memo argument of the next invocation. The last return value will be returned by reduce().
memo
the initial value that will be passed as memo argument to the callback on its very first invocation.
(return value)
the resulting value. If the list was empty, it returns the memo argument.

Description

Reduces the list into a single value with the help of a callback function. This callback will be called once for each element, with the return value of the previous invocation as parameter. reduce() returns the return value of the last invocation.

Example

Sum up some numbers:

var sum = _(1, 2, 3).reduce(function(memo, item, index) { return memo + item; }, 0);

Comments

comments powered by Disqus

back to Overview

Functions