Skip to content

Instantly share code, notes, and snippets.

@ignaces
Created January 11, 2018 17:05
Show Gist options
  • Select an option

  • Save ignaces/9a158cf6d94e5fd775ca920413b554e7 to your computer and use it in GitHub Desktop.

Select an option

Save ignaces/9a158cf6d94e5fd775ca920413b554e7 to your computer and use it in GitHub Desktop.
sumBy loadash function in vanilla javascript
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