-1

I have the following json setup which imitates a folder structure:

    {
    "boxId": "45345344535",
    "modifiedDate": "2023-08-18T11:07:43-04:00",
    "name": "FolderTest",
    "size": 7751630,
    "files": [
        {
            "boxId": "2343214243",
            "modifiedDate": null,
            "name": "Original Preprint Submission.pdf",
            "size": null
        },
        {
            "boxId": "43534534543534",
            "modifiedDate": null,
            "name": "Original Supporting docs.msg",
            "size": null
        }
    ],
    "folders": [
        {
            "boxId": "34534534534535",
            "modifiedDate": "2023-08-18T11:07:02-04:00",
            "name": "Round 1",
            "size": 4092614,
            "files": [
                {
                    "boxId": "45325252435235",
                    "modifiedDate": null,
                    "name": "Round 1 Preprint.pdf",
                    "size": null
                },
                {
                    "boxId": "45436567546754",
                    "modifiedDate": null,
                    "name": "Round 1 response.pdf",
                    "size": null
                },
                {
                    "boxId": "324243245435345",
                    "modifiedDate": null,
                    "name": "Round 1 supporting doc 1.pdf",
                    "size": null
                },
                {
                    "boxId": "3421342142142134",
                    "modifiedDate": null,
                    "name": "Round 1 supporting doc 2.docx",
                    "size": null
                }
            ],
            "folders": []
        }
    ]
}

So I am trying to create a recursive function that takes the id and also the original array then finds the match then removes that file node. I found a similar post here:

Recursively remove object from nested array

But I am struggling to adapt it to my json structure .

1
  • 1
    "I am struggling to adapt it to my json structure" -> can you please share in the question your current code and how it fails? (result if different from expected / error) Commented Aug 18, 2023 at 18:06

2 Answers 2

0

Remove IN-PLACE:

const removeNode = (parent, id) => {
  let idx = parent.files.findIndex(({boxId}) => id === boxId);
  if(idx >= 0){
    parent.files.splice(idx, 1);
    return true;
  }
  for(let i = 0; i < parent.folders.length; i++){
    if(parent.folders[i].boxId === id){
      parent.folders.splice(i, 1);
      return true;
    }
    if(removeNode(parent.folders[i], id)){
      return true;
    }
  }
  return false;
};

console.log('removing 3421342142142134:', removeNode(root, '3421342142142134'));
console.log(root);
<script>
const root = {
 
    "boxId": "45345344535",
    "modifiedDate": "2023-08-18T11:07:43-04:00",
    "name": "FolderTest",
    "size": 7751630,
    "files": [
        {
            "boxId": "2343214243",
            "modifiedDate": null,
            "name": "Original Preprint Submission.pdf",
            "size": null
        },
        {
            "boxId": "43534534543534",
            "modifiedDate": null,
            "name": "Original Supporting docs.msg",
            "size": null
        }
    ],
    "folders": [
        {
            "boxId": "34534534534535",
            "modifiedDate": "2023-08-18T11:07:02-04:00",
            "name": "Round 1",
            "size": 4092614,
            "files": [
                {
                    "boxId": "45325252435235",
                    "modifiedDate": null,
                    "name": "Round 1 Preprint.pdf",
                    "size": null
                },
                {
                    "boxId": "45436567546754",
                    "modifiedDate": null,
                    "name": "Round 1 response.pdf",
                    "size": null
                },
                {
                    "boxId": "324243245435345",
                    "modifiedDate": null,
                    "name": "Round 1 supporting doc 1.pdf",
                    "size": null
                },
                {
                    "boxId": "3421342142142134",
                    "modifiedDate": null,
                    "name": "Round 1 supporting doc 2.docx",
                    "size": null
                }
            ],
            "folders": []
        }
    ]
}
</script>

Sign up to request clarification or add additional context in comments.

1 Comment

doesn't work if trying to remove a root node
0

This should get you started -

function removeId({ files = [], folders = [], ...node }, id){
  return node.boxId === id
    ? []
    : [{ ...node, files: files.flatMap(f => removeId(f, id)), folders: folders.flatMap(f => removeId(f, id)) }]
}

Run the demo to verify in your own browser -

function removeId({ files = [], folders = [], ...node }, id){
  return node.boxId === id
    ? []
    : [{ ...node, files: files.flatMap(f => removeId(f, id)), folders: folders.flatMap(f => removeId(f, id)) }]
}

const node = {boxId: "45345344535",modifiedDate: "2023-08-18T11:07:43-04:00",name: "FolderTest",size: 7751630,files: [{boxId: "2343214243",modifiedDate: null,name: "Original Preprint Submission.pdf",size: null},{boxId: "43534534543534",modifiedDate: null,name: "Original Supporting docs.msg",size: null}],folders: [{boxId: "34534534534535",modifiedDate: "2023-08-18T11:07:02-04:00",name: "Round 1",size: 4092614,files: [{boxId: "45325252435235",modifiedDate: null,name: "Round 1 Preprint.pdf",size: null},{boxId: "45436567546754",modifiedDate: null,name: "Round 1 response.pdf",size: null},{boxId: "324243245435345",modifiedDate: null,name: "Round 1 supporting doc 1.pdf",size: null},{boxId: "3421342142142134",modifiedDate: null,name: "Round 1 supporting doc 2.docx",size: null}],folders: []}]}

console.log(removeId(node, "34534534534535"))
.as-console-wrapper { min-height: 100%; top: 0; }

[
  {
    "boxId": "45345344535",
    "modifiedDate": "2023-08-18T11:07:43-04:00",
    "name": "FolderTest",
    "size": 7751630,
    "files": [
      {
        "boxId": "2343214243",
        "modifiedDate": null,
        "name": "Original Preprint Submission.pdf",
        "size": null,
        "files": [],
        "folders": []
      },
      {
        "boxId": "43534534543534",
        "modifiedDate": null,
        "name": "Original Supporting docs.msg",
        "size": null,
        "files": [],
        "folders": []
      }
    ],
    "folders": []
  }
]

2 Comments

seems quite slow to use flatMap() and copy the whole structure. maybe this is what the OP wants
removeId is written here as a pure function, it does not mutate the input. what might "seem quite slow" is not actually slow, objects are copied by reference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.