Skip to content

Instantly share code, notes, and snippets.

@sany2k8
Forked from gunar/functionalObjSum.js
Created April 29, 2018 17:13
Show Gist options
  • Select an option

  • Save sany2k8/02219d81afe7190494ea2f4cfd279fc6 to your computer and use it in GitHub Desktop.

Select an option

Save sany2k8/02219d81afe7190494ea2f4cfd279fc6 to your computer and use it in GitHub Desktop.
Function to sum an object values using recursion
// 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