Last active
August 10, 2016 18:22
-
-
Save mwurzberger/7dff0022571e773b34907c8a9d17f721 to your computer and use it in GitHub Desktop.
nested object iteration
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
function stripCartId(mainObj) { | |
function nestedSearch(obj) { | |
Object.keys(obj).forEach(function (key) { | |
if (key === 'cart') { | |
delete obj[key].id; | |
} | |
if (typeof obj[key] === 'object') { | |
return nestedSearch(obj[key]); | |
} | |
}); | |
return obj; | |
} | |
function iterObj(obj) { | |
for (var key in obj) { | |
if (typeof obj[key] === "string") { | |
// Do something | |
} | |
if (obj[key] !== null && typeof obj[key] === "object") { | |
// Recurse into children | |
iterObj(obj[key]); | |
} | |
} | |
} | |
return nestedSearch(_.cloneDeep(mainObj)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment