Home

function: reduce


reduce(collection: Array, Object, iteratee: function, accumulator: *): *

Reduces `collection` to a value which is the accumulated result of running each element in `collection` thru `iteratee`, where each successive invocation is supplied the return value of the previous. If `accumulator` is not given, the first element of `collection` is used as the initial value. The iteratee is invoked with four arguments: (accumulator, value, index|key, collection). Many lodash methods are guarded to work as iteratees for methods like `reduce`, `reduceRight`, and `transform`. The guarded methods are: `assign`, `defaults`, `defaultsDeep`, `includes`, `merge`, `orderBy`, and `sortBy`

Example(s):

reduce([1, 2], (sum, n) => sum + n, 0)
// => 3

reduce({ 'a': 1, 'b': 2, 'c': 1 }, (result, value, key) => {
  (result[value] || (result[value] = [])).push(key)
  return result
}, {})
// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)

Params:

Name Type Attribute Description
collection The collection to iterate over.
iteratee The function invoked per iteration.
accumulator The initial value.

Returns:

*

  {
    "comment": "/**\r\n * Reduces `collection` to a value which is the accumulated result of running\r\n * each element in `collection` thru `iteratee`, where each successive\r\n * invocation is supplied the return value of the previous. If `accumulator`\r\n * is not given, the first element of `collection` is used as the initial\r\n * value. The iteratee is invoked with four arguments:\r\n * (accumulator, value, index|key, collection).\r\n *\r\n * Many lodash methods are guarded to work as iteratees for methods like\r\n * `reduce`, `reduceRight`, and `transform`.\r\n *\r\n * The guarded methods are:\r\n * `assign`, `defaults`, `defaultsDeep`, `includes`, `merge`, `orderBy`,\r\n * and `sortBy`\r\n *\r\n * @since 0.1.0\r\n * @category Collection\r\n * @param {Array|Object} collection The collection to iterate over.\r\n * @param {Function} iteratee The function invoked per iteration.\r\n * @param {*} [accumulator] The initial value.\r\n * @returns {*} Returns the accumulated value.\r\n * @see reduceRight, transform\r\n * @example\r\n *\r\n * reduce([1, 2], (sum, n) => sum + n, 0)\r\n * // => 3\r\n *\r\n * reduce({ 'a': 1, 'b': 2, 'c': 1 }, (result, value, key) => {\r\n *   (result[value] || (result[value] = [])).push(key)\r\n *   return result\r\n * }, {})\r\n * // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)\r\n */",
    "meta": {
        "range": [
            1439,
            1676
        ],
        "filename": "reduce.js",
        "lineno": 38,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100007518",
            "name": "reduce",
            "type": "FunctionDeclaration",
            "paramnames": [
                "collection",
                "iteratee",
                "accumulator"
            ]
        },
        "vars": {
            "func": "reduce~func",
            "initAccum": "reduce~initAccum"
        }
    },
    "description": "Reduces `collection` to a value which is the accumulated result of running\reach element in `collection` thru `iteratee`, where each successive\rinvocation is supplied the return value of the previous. If `accumulator`\ris not given, the first element of `collection` is used as the initial\rvalue. The iteratee is invoked with four arguments:\r(accumulator, value, index|key, collection).\r\rMany lodash methods are guarded to work as iteratees for methods like\r`reduce`, `reduceRight`, and `transform`.\r\rThe guarded methods are:\r`assign`, `defaults`, `defaultsDeep`, `includes`, `merge`, `orderBy`,\rand `sortBy`",
    "since": "0.1.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "Collection",
            "value": "Collection"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "Array",
                    "Object"
                ]
            },
            "description": "The collection to iterate over.",
            "name": "collection"
        },
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "description": "The function invoked per iteration.",
            "name": "iteratee"
        },
        {
            "type": {
                "names": [
                    "*"
                ]
            },
            "optional": true,
            "description": "The initial value.",
            "name": "accumulator"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "*"
                ]
            },
            "description": "Returns the accumulated value."
        }
    ],
    "see": [
        "reduceRight, transform"
    ],
    "examples": [
        "reduce([1, 2], (sum, n) => sum + n, 0)\r// => 3\r\rreduce({ 'a': 1, 'b': 2, 'c': 1 }, (result, value, key) => {\r  (result[value] || (result[value] = [])).push(key)\r  return result\r}, {})\r// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)"
    ],
    "name": "reduce",
    "longname": "reduce",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000688",
    "___s": true,
    "filepath": "reduce.js"
}