I am trying to make a generic JSON function that gets sent a source, destination, and if it should delete the empty parent when done. The problem is I'm not sure how to delete the parent object once it is empty and completed through the loop.
My code is successfully taking the children and moving them as well as deleting the children from the current parent. The problem I'm facing is trying to delete the parent itself after it completes.
let j = {
'test': 'test',
'remove': { 'string1' : 'hello', 'string2' : 'world' }
}
moveAll(j.remove,j,true);
function moveAll(src, dst, del) {
del = del || false;
for ( let item in src ) {
dst[item] = src[item];
if(del) {
delete src[item];
}
}
}
With the code above I am trying to take the contents of remove and place them in the main objects location.
So it would be like this if it was working as intended.
let j = {
'test': 'test',
'string1' : 'hello',
'string2' : 'world'
}
This is what I am currently getting. I tried using delete src, but src only contains the contents of remove and not remove itself. So I'm wondering if there is a way to access the parent.
let j = {
'test': 'test',
'remove': { },
'string1' : 'hello',
'string2' : 'world'
}
This is just a base idea of what I want to do so I know there is still lots that needs to be done. But I tried looking this issue up and could not find a solution.
Thanks in advance!