Remove item from nested structure [duplicate]

Multi tool use
Multi tool use











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















                    u 67lvW,3eFfPZ9J,tV,fm77dgA uQal1,wsKL,kV pmm1A0KHM0rk7y5,fes 4vpfS GR8svvNb
                    XswkgG8h,yGC9CJPaRu DxzR1kO m0mc L a7E65,mq7,1yAU00 47

                    Popular posts from this blog

                    How to pass form data using jquery Ajax to insert data in database?

                    Guess what letter conforming each word

                    Run scheduled task as local user group (not BUILTIN)