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 .
3
Answers
You want to create a recursive function that searches for an item with a given
boxId
and removes it from the given JSON structure. Here’s how you can do it:boxId
.Here’s the code:
To use the function:
This function will modify the given
json_data
in-place to remove the item with the specifiedboxId
.This should get you started –
Run the demo to verify in your own browser –
Remove IN-PLACE: