Remove item from nested structure [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • remove object from nested array

    4 answers




I have the following object:



{
id: 1,
children: [
{id: 2},
{id: 3, children: [
{id: 4 }
]}
]
}


I want to be able to remove a specific object by using the id property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.










share|improve this question













marked as duplicate by ponury-kostek, Nina Scholz javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 9:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    -1
    down vote

    favorite













    This question already has an answer here:




    • remove object from nested array

      4 answers




    I have the following object:



    {
    id: 1,
    children: [
    {id: 2},
    {id: 3, children: [
    {id: 4 }
    ]}
    ]
    }


    I want to be able to remove a specific object by using the id property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.










    share|improve this question













    marked as duplicate by ponury-kostek, Nina Scholz javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 8 at 9:44


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite












      This question already has an answer here:




      • remove object from nested array

        4 answers




      I have the following object:



      {
      id: 1,
      children: [
      {id: 2},
      {id: 3, children: [
      {id: 4 }
      ]}
      ]
      }


      I want to be able to remove a specific object by using the id property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.










      share|improve this question














      This question already has an answer here:




      • remove object from nested array

        4 answers




      I have the following object:



      {
      id: 1,
      children: [
      {id: 2},
      {id: 3, children: [
      {id: 4 }
      ]}
      ]
      }


      I want to be able to remove a specific object by using the id property (e.g. remove the object with an id of 4). I can obviously find the object by using arr.findIndex but how do I remove it from the enclosing array? I'm struggling with finding how to get the parent array so I can remove it. Again I know how to remove from an array using splice but the problem I'm having is how to do this programmatically so I can remove any item from anywhere in a nested structure as above.





      This question already has an answer here:




      • remove object from nested array

        4 answers








      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 9:26









      Mike Rifgin

      3,054135598




      3,054135598




      marked as duplicate by ponury-kostek, Nina Scholz javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 8 at 9:44


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by ponury-kostek, Nina Scholz javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 8 at 9:44


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Try following






          let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};

          /* o is object or sub-object
          * k is id to be deleted
          * p is parent of object, same as o for first time
          * index is the index of item in the children array */
          function removeKey(o, k, p=o, index) {
          if(o.id === k) { // if this is the object that needs to be removed
          // For first level object, make the object empty
          if(o.id === p.id) {delete o.children; delete o.id}
          // For others remove it from the children array
          else p.children.splice(index,1);
          } else if(o.children) { // if the object is not a match and has children
          // iterate over the children and check and remove key
          for (let i = 0; i < o.children.length; i++) {
          if(removeKey(o.children[i], k, o, i)) break;
          }
          }
          }

          removeKey(obj, 4);
          console.log(obj);








          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            Try following






            let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};

            /* o is object or sub-object
            * k is id to be deleted
            * p is parent of object, same as o for first time
            * index is the index of item in the children array */
            function removeKey(o, k, p=o, index) {
            if(o.id === k) { // if this is the object that needs to be removed
            // For first level object, make the object empty
            if(o.id === p.id) {delete o.children; delete o.id}
            // For others remove it from the children array
            else p.children.splice(index,1);
            } else if(o.children) { // if the object is not a match and has children
            // iterate over the children and check and remove key
            for (let i = 0; i < o.children.length; i++) {
            if(removeKey(o.children[i], k, o, i)) break;
            }
            }
            }

            removeKey(obj, 4);
            console.log(obj);








            share|improve this answer

























              up vote
              1
              down vote













              Try following






              let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};

              /* o is object or sub-object
              * k is id to be deleted
              * p is parent of object, same as o for first time
              * index is the index of item in the children array */
              function removeKey(o, k, p=o, index) {
              if(o.id === k) { // if this is the object that needs to be removed
              // For first level object, make the object empty
              if(o.id === p.id) {delete o.children; delete o.id}
              // For others remove it from the children array
              else p.children.splice(index,1);
              } else if(o.children) { // if the object is not a match and has children
              // iterate over the children and check and remove key
              for (let i = 0; i < o.children.length; i++) {
              if(removeKey(o.children[i], k, o, i)) break;
              }
              }
              }

              removeKey(obj, 4);
              console.log(obj);








              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Try following






                let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};

                /* o is object or sub-object
                * k is id to be deleted
                * p is parent of object, same as o for first time
                * index is the index of item in the children array */
                function removeKey(o, k, p=o, index) {
                if(o.id === k) { // if this is the object that needs to be removed
                // For first level object, make the object empty
                if(o.id === p.id) {delete o.children; delete o.id}
                // For others remove it from the children array
                else p.children.splice(index,1);
                } else if(o.children) { // if the object is not a match and has children
                // iterate over the children and check and remove key
                for (let i = 0; i < o.children.length; i++) {
                if(removeKey(o.children[i], k, o, i)) break;
                }
                }
                }

                removeKey(obj, 4);
                console.log(obj);








                share|improve this answer












                Try following






                let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};

                /* o is object or sub-object
                * k is id to be deleted
                * p is parent of object, same as o for first time
                * index is the index of item in the children array */
                function removeKey(o, k, p=o, index) {
                if(o.id === k) { // if this is the object that needs to be removed
                // For first level object, make the object empty
                if(o.id === p.id) {delete o.children; delete o.id}
                // For others remove it from the children array
                else p.children.splice(index,1);
                } else if(o.children) { // if the object is not a match and has children
                // iterate over the children and check and remove key
                for (let i = 0; i < o.children.length; i++) {
                if(removeKey(o.children[i], k, o, i)) break;
                }
                }
                }

                removeKey(obj, 4);
                console.log(obj);








                let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};

                /* o is object or sub-object
                * k is id to be deleted
                * p is parent of object, same as o for first time
                * index is the index of item in the children array */
                function removeKey(o, k, p=o, index) {
                if(o.id === k) { // if this is the object that needs to be removed
                // For first level object, make the object empty
                if(o.id === p.id) {delete o.children; delete o.id}
                // For others remove it from the children array
                else p.children.splice(index,1);
                } else if(o.children) { // if the object is not a match and has children
                // iterate over the children and check and remove key
                for (let i = 0; i < o.children.length; i++) {
                if(removeKey(o.children[i], k, o, i)) break;
                }
                }
                }

                removeKey(obj, 4);
                console.log(obj);





                let obj = {id: 1,children: [{id: 2},{id: 3, children: [{id: 4 }]}]};

                /* o is object or sub-object
                * k is id to be deleted
                * p is parent of object, same as o for first time
                * index is the index of item in the children array */
                function removeKey(o, k, p=o, index) {
                if(o.id === k) { // if this is the object that needs to be removed
                // For first level object, make the object empty
                if(o.id === p.id) {delete o.children; delete o.id}
                // For others remove it from the children array
                else p.children.splice(index,1);
                } else if(o.children) { // if the object is not a match and has children
                // iterate over the children and check and remove key
                for (let i = 0; i < o.children.length; i++) {
                if(removeKey(o.children[i], k, o, i)) break;
                }
                }
                }

                removeKey(obj, 4);
                console.log(obj);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 9:37









                Nikhil Aggarwal

                22.9k32647




                22.9k32647















                    Popular posts from this blog

                    鏡平學校

                    ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                    Why https connections are so slow when debugging (stepping over) in Java?