Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Recursively Delete objects from an array of nested objects

ladenG

New Coder
Anyone know how to recursively find and delete an object from an array structured like this with infinite nested objects with the same structure?

JavaScript:
[
  {
    id: 1,
    nest: [
      {
        id: 2,
        nest: [{id: 3, nest: [{..continues..}]}],
      },
      {
        id: 4,
        nest: [{id: 5, nest: [{..continues..}]}],
      },
    ],
  },
  {...continues..}
];

My bad attempt at this:
JavaScript:
const handleDelete = (id: number) => {
  if (objToFindAndDelete) {
    const tempObj = JSON.parse(JSON.stringify(objToFindAndDelete)) as TObjStructure;
    const findAndDelete = (menu: TObjStructure, id: number) => {
      menu.map((item, index) => {
        if (item.menu_id === id) {
          const index = tempObj.indexOf(item);
          tempObj.splice(index, 1);
        } else if (item.nest && item.nest.length > 0) {
          findAndDelete(item.nest, id);
        }
      });
    };
    findAndDelete(tempObj, id);
  }
};

Is there a way to find the object by id and then delete that specific object only? Thanks!
 
To recursively find and delete an object from an array structured like the one you provided, you can use a function that takes in the array and the id of the object to be deleted. Here's an example implementation:
Code:
function deleteObjectById(arr: any[], id: number) {
  for (let i = 0; i < arr.length; i++) {
    const obj = arr[i];
    if (obj.id === id) {
      // If the object is found, remove it from the array
      arr.splice(i, 1);
      return true;
    } else if (obj.nest && obj.nest.length > 0) {
      // If the object has nested objects, recursively call this function on the nested array
      const deleted = deleteObjectById(obj.nest, id);
      if (deleted) {
        return true;
      }
    }
  }
  return false;
}
This function takes in the array and the id of the object to be deleted. It then iterates through the array, checking each object to see if its id matches the id that was passed in. If it finds a match, it removes that object from the array and returns true. If the object has nested objects, it recursively calls this function on the nested array, and returns true if it successfully deletes the object. If it doesn't find the object in the array, it returns false.

To use this function, you can call it like this:
Code:
const arr = [{ id: 1, nest: [{ id: 2, nest: [{ id: 3, nest: [] }] }] }];
deleteObjectById(arr, 2);
console.log(arr);
This will delete the object with id 2 from the array and log the updated array to the console.
 

Buy us a coffee!

Back
Top Bottom