Created
March 18, 2013 00:16
-
-
Save tgvashworth/5184229 to your computer and use it in GitHub Desktop.
This file contains 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
// 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