Skip to content Skip to sidebar Skip to footer

Delete Nested Javascript Object

I have been trying to delete a nested object from a JavaScript object, with no success and have not been able to find the correct answer through searching previous posts. Here is

Solution 1:

You need a recursive function to delete the property.

var myobj = {
  "children": [
    {
      "name": "albuterol ",
      "children": [
        {
          "name": "albuterol  - fluticasone ",
          "children": [
            {
              "name": "prednisone ",
              "children": [
                {
                  "name": "dexamethasone ",
                  "children": [],
                  "size": 1,
                  "colname": "CONCEPT_NAME.4"
                }
              ],
              "size": 3,
              "colname": "CONCEPT_NAME.3"
            }
          ],
          "size": 4,
          "colname": "CONCEPT_NAME.2"
        }]}]} 

functiondeleteColnameRecursive(obj){
  delete obj.colnameif(obj.children){
     for(var i=0;i<obj.children.length;i++)
         deleteColnameRecursive(obj.children[i]);
  }
}

deleteColnameRecursive(myobj);

console.log(myobj);

Solution 2:

MyObj does not directly have a property of colname. MyObj has an array named Children.

To delete the proper attribute, select the proper object. For example myObj.children[0].colname

Post a Comment for "Delete Nested Javascript Object"