Delete an item according to id in the js recursive object

  1. input ID and a recursive object to delete the item with ID
  2. the function I wrote is as follows
      function deleteNode(paramId,obj){
        console.log("enterDelete",obj)
        for(let o in obj){
          if(obj[o].paramId==paramId){
            console.log("",obj)
            obj.splice(o,1)
            return obj
          }
          else{
            if(obj[o].hasOwnProperty("subList")&&obj[o].subList.length!=0){
              console.log(4,JSON.parse(JSON.stringify(obj[o].subList)))
              let m=JSON.parse(JSON.stringify(obj[o].subList))
              deleteNode(paramId,m)
            }
          }
        }

      }

expect

obj={
    "paramId":"",
    "sourcePath":"",
    "subList":[
        {
            "paramId":500064,
            "sourcePath":"Result",
            "subList":[
                {
                    "paramId":500061,
                    "sourcePath":"Partners",
                    "subList":[

                    ]
                }
            ]
        }
    ]
}

paramId=500061

//  

{
    "paramId":"",
    "sourcePath":"",
    "subList":[
        {
            "paramId":500064,
            "sourcePath":"Result",
            "subList":[
              //  
            ]
        }
    ]
}
Apr.09,2021

refer to


function mydelete(id,obj){
  if(obj.paramId===id){
    return true;
  }
  let pure = [];
  for(let i of obj.subList){
    if(mydelete(id,i)){
    }else{
      pure.push(i);
    }
  }
  obj.subList = pure;
  return false;
}

the main problem is that you cannot delete yourself after recursively going to the next layer, so just notify the upper layer to delete it recursively.
the method of deletion here, I directly replace it with arr. If you have other methods, you can, too. Just make sure that the array is not out of bounds when you delete it in a loop.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea3031-466a2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea3031-466a2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?