Remove JSON parent object after changing the location of all children

Viewed 301

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!

3 Answers

You cannot delete the parent from your moveAll function (as written) because it doesn't have any reference to the j object. (You pass in j.remove, but it doesn't have j itself.)

This would work:

moveAll(j, 'remove', j, true);

function moveAll(obj, key, dst, del) {
    src = obj[key]
    del = del || false;
    for ( let item in src ) {
        dst[item] = src[item];
        if(del) {
            delete src[item];
        }
    }

    if (del) 
        delete obj[key]
}

Now your function has access to the parent object and can easily just do a delete obj[key] at the end.

One alternative can be to loop through all keys of dst and check if any of them point to src. If yes, remove that key from dst (note that this assumes src is a direct child of dst).

function moveAll(src, dst, del) {
  del = del || false;
  for (let item in src) {
    dst[item] = src[item];
  }
  if (del) {
    for (let k in dst) {
      if (dst[k] === src) delete dst[k];
    }
  }
}

let j = {
  'test': 'test',
  'remove': {
    'string1': 'hello',
    'string2': 'world'
  }
}

moveAll(j.remove, j, true);
console.log(j);

Once you pass them into the function src and dst are 2 different objects, so using the delete on src won't buy you anything. You need to delete dst.remove like below, but you need to find a way to reference it within the function.

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];
    }
}
delete dst.remove;
}
Related