Skip to content

Instantly share code, notes, and snippets.

@mwurzberger
Last active August 10, 2016 18:22
Show Gist options
  • Save mwurzberger/7dff0022571e773b34907c8a9d17f721 to your computer and use it in GitHub Desktop.
Save mwurzberger/7dff0022571e773b34907c8a9d17f721 to your computer and use it in GitHub Desktop.
nested object iteration
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