I've come to the conclusion that this is near impossible. In the picture below, I am trying to remove the object with the key of 36 without knowing the key of its parent(s).
I was able to find the object with the key of 36 using the the following function (where o is the object and id is the key), but, then, there's no way I can see to remove the child object from the object.
function findById(o, id) {
//Early return
if( o.id === id ){
return o;
}
var result, p;
for (p in o) {
if( o.hasOwnProperty(p) && typeof o[p] === 'object' ) {
result = findById(o[p], id);
if(result){
return result;
}
}
}
return result;
}
delete o[p]
If you can find it, you can find its parent. If you can find its parent, you can delete the key you're looking for. You can modify your function to do just that. It will return true
on success.
function deleteId(o, id) {
if(o.hasOwnProperty(id)) {
return delete o[id];
}
var p;
for(p in o) {
if(o.hasOwnProperty(p) && typeof o[p] === 'object') {
var success = deleteId(o[p], id);
if(success) {
return true;
}
}
}
return false;
}