I have a react n-level app where the delete
function is a recursive function utilizing Array.prototype.filter()
. The data is an array of objects and in each object there can be an array that contains more data.
Data:
[
{
"id": 1,
"hasParent": false,
"hasChildren": true,
"parentId": 0,
"childrenIds": [
3,
5
],
"text": "Opportunities don't happen, you create them.",
"childComments": [
{
"id": 3,
"hasParent": true,
"hasChildren": true,
"parentId": 1,
"childrenIds": [
4
],
"text": "one",
"childComments": [
{
"id": 4,
"hasParent": true,
"hasChildren": false,
"parentId": 3,
"childrenIds": [],
"text": "two",
"childComments": []
}
]
},
{
"id": 5,
"hasParent": true,
"hasChildren": false,
"parentId": 1,
"childrenIds": [],
"text": "one",
"childComments": []
}
]
},
{
"id": 2,
"hasParent": false,
"hasChildren": false,
"parentId": 0,
"childrenIds": [],
"text": "Just one small positive thought in the morning can change your whole day.",
"childComments": []
},
{
"id": 6,
"hasParent": false,
"hasChildren": false,
"parentId": 0,
"childrenIds": [],
"text": "More data one",
"childComments": []
},
{
"id": 7,
"hasParent": false,
"hasChildren": true,
"parentId": 0,
"childrenIds": [
8
],
"text": "More data two",
"childComments": [
{
"id": 8,
"hasParent": true,
"hasChildren": false,
"parentId": 7,
"childrenIds": [],
"text": "More data two-two",
"childComments": []
}
]
}
]
The delete
function works for the top level of the data structure. For nested data, the delete
function is not working. What am I doing wrong?
This is the code for the recursive delete function:
const HandleDelete = function(commentObj, objId) {
const filteredObj = commentObj.filter((ele) =>{
if (ele.childComments.length > 0 ) {
HandleDelete(ele.childComments, objId)
}
return ele.id !== objId;
})
return filteredObj;
}
4
Answers
As mentioned by @Pointy, you aren’t doing anything with the result of the inner
HandleDelete
.What I think you want is to map over the childComments first, then filter
EDIT: changed order of map and filter, so it deletes top level items first, so it doesn’t waste time recursing them only to throw away the result
Looks like you aren’t updating the
childComments
property of the parent object after removing the child comment. Try updating thechildComments
property on the parent comment by calling yourhandleDelete
function recursively, and setting the result as the newchildComments
value:When you modify an element inside the filter function, it can cause unexpected behavior and may produce incorrect results. This is because modifying an element can change the truthiness of the condition you are using to filter, which can lead to unexpected filtering results.
A better way might be this
Just filter without recursion, and only then apply recursion to childComments, where present: