Skip to content

Instantly share code, notes, and snippets.

@moo3
Created September 3, 2015 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moo3/e45a1d823e6d45e974f5 to your computer and use it in GitHub Desktop.
Save moo3/e45a1d823e6d45e974f5 to your computer and use it in GitHub Desktop.
var o = {
first: {
a: 1,
b: 2,
c: {
second: {
a: 1,
b: {
third: {
a: 1
}
}
}
}
},
second: {
a: 1,
b: {
second: {
third: {
fourth: {
a: 1,
b: 2,
fifth: {
a: 1
}
}
}
}
}
}
}
function _isObject(val) {
return typeof val == 'object';
}
function _destroyer(source,key,path) {
delete source[key];
console.log('destroyed ',path);
}
function deepDelete(obj, key, parent, path) {
var p = path ? path +'.'+ key : key;
if(!_isObject(obj)) {
_destroyer(parent, key, p);
return Object.keys(parent);
}
for(var i in obj) {
if(obj.hasOwnProperty(i)) {
if(!deepDelete(obj[i], i, obj, p)) {
_destroyer(obj, i, p +'.'+ i);
}
}
}
if(key === 'ROOT') {
return obj;
}
}
deepDelete(o, 'ROOT');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment