Skip to content

Instantly share code, notes, and snippets.

@tgvashworth
Created March 18, 2013 00:16
Show Gist options
  • Save tgvashworth/5184229 to your computer and use it in GitHub Desktop.
Save tgvashworth/5184229 to your computer and use it in GitHub Desktop.
// Recursively sum integer properties of an object
var sumObject = function (source, target) {
Object.keys(source).forEach(function (key) {
if (typeof source[key] === "object") {
if (!target[key]) target[key] = {};
target[key] = sumObject(source[key], target[key]);
}
if (typeof source[key] === "number") {
if (!target[key]) target[key] = 0;
target[key] += source[key];
}
});
return target;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment