Delete object in array by parent id recursively

HOE INN

Currently I'm working on recursive methods.

'The array' has 'objects' which have parentId and Id for itself.

I wanna make a function: when I choose one object, wanna delete every child object by parentId. also at the same time child's child should be deleted.

Now this code works on some child objects, but not for all. and I don't know why does it not working.

Could you please help me to figure out this problem?

    function deleteMindMap(obj) {


        alert('Before Delete : ' + JSON.stringify(savedArray));

        savedArray = deleteUsingParentId(savedArray, obj.id);           

        alert('After Delete : ' + JSON.stringify(savedArray));

        //Rewriting to firebase
        mindRef.remove();
        writeMindMap(savedArray);



    }

function deleteUsingParentId(data, parentId) {
    var updatedArray = savedArray.filter((item) => {

    return item.parent !=  parentId;

    })

    return updatedArray;
}

tried the code but still not working on parent object and child's child object.

this is the result of the code when I deleted the object of id '12'

cause '123', '1234' have parent id '12',

'123456789 1234567890' , '12345, 123456' should be deleted

cause they are child of '123' and '1234'

and also '12' is not deleted.

Before Delete : [
{"afterX":485,"afterY":271,"id":"1","kind":"line","parent":"1","x":448,"y":220},
{"afterX":643,"afterY":276,"id":"12","kind":"line","parent":"1","x":490,"y":278},
{"afterX":732,"afterY":238,"id":"123","kind":"line","parent":"12","x":659,"y":283},{"afterX":708,"afterY":413,"id":"1234","kind":"line","parent":"12","x":668,"y":291},
{"afterX":847,"afterY":390,"id":"12345","kind":"line","parent":"1234","x":721,"y":418},
{"afterX":791,"afterY":494,"id":"123456","kind":"line","parent":"1234","x":715,"y":427},
{"afterX":904,"afterY":520,"id":"1234567","kind":"line","parent":"123456","x":810,"y":503},
{"afterX":944,"afterY":301,"id":"12345678","kind":"line","parent":"1234567","x":913,"y":521},
{"afterX":796,"afterY":136,"id":"123456789","kind":"line","parent":"123","x":736,"y":230},
{"afterX":869,"afterY":227,"id":"1234567890","kind":"line","parent":"123","x":752,"y":245}]


After Delete : [{"afterX":485,"afterY":271,"id":"1","kind":"line","parent":"1","x":448,"y":220},
{"afterX":643,"afterY":276,"id":"12","kind":"line","parent":"1","x":490,"y":278},
{"afterX":847,"afterY":390,"id":"12345","kind":"line","parent":"1234","x":721,"y":418},
{"afterX":791,"afterY":494,"id":"123456","kind":"line","parent":"1234","x":715,"y":427},
{"afterX":904,"afterY":520,"id":"1234567","kind":"line","parent":"123456","x":810,"y":503},
{"afterX":944,"afterY":301,"id":"12345678","kind":"line","parent":"1234567","x":913,"y":521},
{"afterX":796,"afterY":136,"id":"123456789","kind":"line","parent":"123","x":736,"y":230},
{"afterX":869,"afterY":227,"id":"1234567890","kind":"line","parent":"123","x":752,"y":245}
]
Axel

I think this should do what you're looking for. It may break if you have circular dependencies. It still uses recursion but I moved the recursion inside the filter function.

function deleteChild(id){
    function filter(_target){
        let toDelete = [];

        for(let i = 0; i < savedArray.length; i++){
            if(savedArray[i].id == _target || savedArray[i].parent == _target){
                toDelete.push(i);
                if(savedArray[i].id != _target){
                    toDelete = toDelete.concat(filter(savedArray[i].id).slice(1));
                }
            }
        }

        return toDelete;
    }
    const targets = filter(id).sort();
    for(let i = targets.length - 1; i >= 0; i--){
        savedArray.splice(targets[i],1);
    }
}

In your example you omitted 1234567 and 12345678 who should also get deleted. 1234567 is a child of 123456, who got deleted and 12345678 is a child of 1234567

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not able to push an object into parent array by identifying the parent id of the object in javascript

From Dev

Delete object by id from array Ramda

From Dev

Delete object from array by given _id in in Mongoose

From Dev

PHP recursively delete empty parent directories

From Dev

How to delete mongoose array item not knowing its parent id?

From Dev

condensing the array object recursively in javascript

From Dev

Add object to array recursively in JavaScript

From Dev

recursively remove undefined from object (including parent)

From Dev

Recursively delete a list of values from object

From Dev

How to access the parent index id in an Object array in Angular?

From Dev

Recursively filter and delete item in array of objects

From Dev

If the 'id' key is duplicated among the objects in the array, how to delete the object

From Dev

xmlstarlet delete parent elements from multiple documents recursively

From Dev

delete folders with the same name as the parent folders recursively in bash?

From Dev

Delete specific object with id

From Java

How to clone an object and recursively set Id to Null?

From Dev

Recursively get the 'id' keys of a nested object in Javascript

From Dev

Recursively loop throught multidemensional array and keep track of parent array

From Dev

Find an object in an array of deeply nested objects recursively

From Dev

How to recursively flatten and merge nested array/object

From Dev

Map typescript array elements to keyof object, recursively

From Dev

delete object in array

From Dev

How to delete object in array?

From Dev

Delete object from array

From Dev

Delete element in an array of object

From Dev

Delete same object in an array

From Dev

how do i delete child element in nested Json Array and rename Id of child element and merge it to parent element?

From Dev

How to get immediate parent Id of the child id in array of nested json Object?

From Dev

Delete objects from the array of object which reference id did not matches with any other object id from the same array of objects in javascript

Related Related

  1. 1

    Not able to push an object into parent array by identifying the parent id of the object in javascript

  2. 2

    Delete object by id from array Ramda

  3. 3

    Delete object from array by given _id in in Mongoose

  4. 4

    PHP recursively delete empty parent directories

  5. 5

    How to delete mongoose array item not knowing its parent id?

  6. 6

    condensing the array object recursively in javascript

  7. 7

    Add object to array recursively in JavaScript

  8. 8

    recursively remove undefined from object (including parent)

  9. 9

    Recursively delete a list of values from object

  10. 10

    How to access the parent index id in an Object array in Angular?

  11. 11

    Recursively filter and delete item in array of objects

  12. 12

    If the 'id' key is duplicated among the objects in the array, how to delete the object

  13. 13

    xmlstarlet delete parent elements from multiple documents recursively

  14. 14

    delete folders with the same name as the parent folders recursively in bash?

  15. 15

    Delete specific object with id

  16. 16

    How to clone an object and recursively set Id to Null?

  17. 17

    Recursively get the 'id' keys of a nested object in Javascript

  18. 18

    Recursively loop throught multidemensional array and keep track of parent array

  19. 19

    Find an object in an array of deeply nested objects recursively

  20. 20

    How to recursively flatten and merge nested array/object

  21. 21

    Map typescript array elements to keyof object, recursively

  22. 22

    delete object in array

  23. 23

    How to delete object in array?

  24. 24

    Delete object from array

  25. 25

    Delete element in an array of object

  26. 26

    Delete same object in an array

  27. 27

    how do i delete child element in nested Json Array and rename Id of child element and merge it to parent element?

  28. 28

    How to get immediate parent Id of the child id in array of nested json Object?

  29. 29

    Delete objects from the array of object which reference id did not matches with any other object id from the same array of objects in javascript

HotTag

Archive