JavaScript- Remove object from parent array with an object's method

Jack Guy

I have a parent object that stores an array of children and calls some of their methods.

var Parent = function ()
{
     this.children = []
     this.addChildren();
}

Parent.prototype.addChildren = function ()
{
     for (var i=0; i < 5; i++)
     {
         this.children.push(new Child());
     }

     this.alterChildren();
}

Parent.prototype.alterChildren = function ()
{
     this.children.forEach(function (child)
     {
         if (child.hasSomeProperty.foo)
         {
              child.alter();
         }
     });
}

Then there are the child objects. When a certain event happens with them, I need them to be effectively destroyed and I null properties that the parent relies on.

var Child = function ()
{
   this.hasSomeProperty = {
      foo: 'bar'
   };
}

Child.prototype.onDestroyEvent = function ()
{
   this.hasSomeProperty = null;
}

I then want to remove this child from the parent's child array and have the child garbage collected. Is there an elegant way to do this without circular references or breaking my existing structures?

Tomalak

If you want the child to send a message to the parent then the child needs to have a reference to the parent.

Parent.prototype.addChildren = function ()
{
    for (var i=0; i < 5; i++)
    {
        this.children.push(new Child(this));
    }
    this.alterChildren();
}
Parent.prototype.removeChild = function (child)
{
    var i = this.children.indexOf(child);
    return this.children.splice(i, 1);
}

and

var Child = function (parent)
{
   this.parent = parent;
   this.hasSomeProperty = {
      foo: 'bar'
   };
}

Child.prototype.destroy = function ()
{
   this.hasSomeProperty = null;    
   this.parent.removeChild(this);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i remove nested or parent object from array in javascript

From Dev

Remove array from javascript object

From Dev

remove an object from an object array in javascript

From Javascript

Access parent's parent from javascript object

From Dev

Remove parent from object

From Dev

Call "private" method from "parent" object in javascript

From Dev

How to remove child object from parent object list javascript

From Dev

JS: Remove object from nested array and return parent array

From Dev

Remove entire object from a json array javascript

From Dev

Javascript fastest way to remove Object from Array

From Dev

Remove empty object from an array javascript

From Dev

Remove multiple object from array Javascript

From Javascript

Remove Object from Array using JavaScript

From Dev

remove and replace item from object in array javascript

From Dev

In Javascript remove keys from object not in an Array

From Dev

Javascript Remove object from array not removing the selected

From Dev

How to remove "integer" array from object in JavaScript

From Dev

Need to access the array of Child object from the Parent object in Javascript

From Dev

Calling javascript Service/Method from array object

From Dev

Remove an object from an array

From Dev

Remove object from array

From Dev

Javascript remove object from array using object variable

From Dev

Javascript remove object from array if object contains string

From Dev

How to remove duplicate object based on condition from an array of object in javascript

From Dev

Javascript: Remove object from array on function call to object

From Dev

Javascript to remove an object from an array if one element in the object contains a string

From Dev

Javascript Object to get this of parent method

From Dev

Remove Object from Array of objects with Parent Child structure

From Dev

remove object from array with just the object's reference

Related Related

  1. 1

    How can i remove nested or parent object from array in javascript

  2. 2

    Remove array from javascript object

  3. 3

    remove an object from an object array in javascript

  4. 4

    Access parent's parent from javascript object

  5. 5

    Remove parent from object

  6. 6

    Call "private" method from "parent" object in javascript

  7. 7

    How to remove child object from parent object list javascript

  8. 8

    JS: Remove object from nested array and return parent array

  9. 9

    Remove entire object from a json array javascript

  10. 10

    Javascript fastest way to remove Object from Array

  11. 11

    Remove empty object from an array javascript

  12. 12

    Remove multiple object from array Javascript

  13. 13

    Remove Object from Array using JavaScript

  14. 14

    remove and replace item from object in array javascript

  15. 15

    In Javascript remove keys from object not in an Array

  16. 16

    Javascript Remove object from array not removing the selected

  17. 17

    How to remove "integer" array from object in JavaScript

  18. 18

    Need to access the array of Child object from the Parent object in Javascript

  19. 19

    Calling javascript Service/Method from array object

  20. 20

    Remove an object from an array

  21. 21

    Remove object from array

  22. 22

    Javascript remove object from array using object variable

  23. 23

    Javascript remove object from array if object contains string

  24. 24

    How to remove duplicate object based on condition from an array of object in javascript

  25. 25

    Javascript: Remove object from array on function call to object

  26. 26

    Javascript to remove an object from an array if one element in the object contains a string

  27. 27

    Javascript Object to get this of parent method

  28. 28

    Remove Object from Array of objects with Parent Child structure

  29. 29

    remove object from array with just the object's reference

HotTag

Archive