Created
September 3, 2015 15:50
-
-
Save moo3/e45a1d823e6d45e974f5 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
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