Add Collection.reduce

This commit is contained in:
Schuyler Cebulskie
2016-09-12 21:33:29 -04:00
parent 0bd00b12b7
commit 84b33f9b66
2 changed files with 14 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@@ -210,6 +210,19 @@ class Collection extends Map {
}
return true;
}
/**
* Identical to
* [Array.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
* @param {function} fn Function used to reduce
* @param {*} [startVal] The starting value
* @returns {*}
*/
reduce(fn, startVal) {
let currentVal = startVal;
for (const [key, val] of this) currentVal = fn(currentVal, val, key, this);
return currentVal;
}
}
module.exports = Collection;