Last active
April 29, 2018 17:13
-
-
Save gunar/0d148814e740c1dd6b8f to your computer and use it in GitHub Desktop.
Function to sum an object values using recursion
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
// This is super slow though: http://jsperf.com/summing-objects/2 | |
var sumObj = function (object) { | |
if (Object.keys(object).length) { | |
var firstKey = Object.keys(object)[0]; | |
var clone = Object.assign({}, object); | |
delete clone[firstKey]; | |
return parseInt(object[firstKey]) + sumObj(clone); | |
} | |
return 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks even neater with ES6: