Created
January 11, 2018 17:05
-
-
Save ignaces/9a158cf6d94e5fd775ca920413b554e7 to your computer and use it in GitHub Desktop.
sumBy loadash function in vanilla javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const baseSum = function(array, iteratee) { | |
| let result = undefined; | |
| for (let value of Array.from(array)) { | |
| const current = iteratee(value); | |
| if (current !== undefined) { | |
| result = result === undefined ? current : result + current; | |
| } | |
| } | |
| return result; | |
| }; | |
| const sumBy = function(array, iteratee) { | |
| if ((array !== null) && array.length) { return baseSum(array, iteratee); } else { return 0; } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment