-2

I'm having an array with nesting array and object as below. Now, with specific id, how can I remove an object and it's children ?

lets say I want to remove an object whose id is 30. So the final out will be the array which will not have object containing id=30

let data= [{"name": "Corporate","id": 1,"editMode": true,"children": [{"name": "Banner","id": 2,"parentId": 1,"editMode": true,"children": [{"name": "Division","id": 3,"parentId": 2,"editMode": false,"children": [{"name": "Region","id": 4,"editMode": true,"children": [{"name": "District","id": 5,"editMode": true,"children": [{"name": "Store","id": 6,"editMode": false,"children": []}]}]}]},{"name": "Banner1","id": 30,"editMode": true,"children": [{"name": "Banner11","id": 35,"editMode": true,"children": []}]},{"name": "Banner1","id": 31,"editMode": true,"children": []},{"name": "Banner1","id": 32,"editMode": true,"children": [{"name": "Banner11","id": 33,"editMode": true,"children": []},{"name": "Banner11","id": 34,"editMode": true,"children": []}]}]},{"name": "Corporate1","id": 36,"editMode": true,"children": [{"name": "Corporate11","id": 38,"editMode": true,"children": []},{"name": "Corporate11","id": 39,"editMode": true,"children": []}]},{"name": "Corporate1","id": 37,"editMode": true,"children": []}]}];

In the below snipped I was able to get the nested object, But how can I delete it from the array?

let data= [{"name": "Corporate","id": 1,"editMode": true,"children": [{"name": "Banner","id": 2,"parentId": 1,"editMode": true,"children": [{"name": "Division","id": 3,"parentId": 2,"editMode": false,"children": [{"name": "Region","id": 4,"editMode": true,"children": [{"name": "District","id": 5,"editMode": true,"children": [{"name": "Store","id": 6,"editMode": false,"children": []}]}]}]},{"name": "Banner1","id": 30,"editMode": true,"children": [{"name": "Banner11","id": 35,"editMode": true,"children": []}]},{"name": "Banner1","id": 31,"editMode": true,"children": []},{"name": "Banner1","id": 32,"editMode": true,"children": [{"name": "Banner11","id": 33,"editMode": true,"children": []},{"name": "Banner11","id": 34,"editMode": true,"children": []}]}]},{"name": "Corporate1","id": 36,"editMode": true,"children": [{"name": "Corporate11","id": 38,"editMode": true,"children": []},{"name": "Corporate11","id": 39,"editMode": true,"children": []}]},{"name": "Corporate1","id": 37,"editMode": true,"children": []}]}];
console.log(findNestedObj(data, 'id', 30));

function findNestedObj(entireObj, keyToFind, valToFind) {
  let foundObj;
  JSON.stringify(entireObj, (_, nestedValue) => {
    if (nestedValue && nestedValue[keyToFind] === valToFind) {
      foundObj = nestedValue;
    }
    return nestedValue;
  });
  return foundObj;
};

5
  • 2
    Get familiar with how to access and process objects and arrays, and use the static and instance methods of Object and Array. Then, edit your question and provide a minimal reproducible example along with your desired results, your actual results, and demonstrate your research and your attempts and explain what precisely didn’t work. Where in the process of writing your code are you stuck? Commented Dec 19, 2022 at 17:09
  • @SebastianSimon edited it with my try, but i'm not able to delete that object Commented Dec 19, 2022 at 17:20
  • 1
    Strange, in your desired output there is still the object with id 30. Also your code seems to be some attempt to find an object, not to remove it. Are you looking to find it? Commented Dec 19, 2022 at 17:20
  • @trincot I tried to remove that object, but was not able to do that. Tried reverse engineering so I made a try to find that object in the nesting array of objects. now I want to delete that from my original array Commented Dec 19, 2022 at 17:23
  • If you tried to remove that object, and not able to, then it would be appropriate that you would include the code of that attempt. In your current code there is no trace of an attempt to remove it. Commented Dec 19, 2022 at 17:36

1 Answer 1

0

Here is a solution where you pass a callback function -- much like the native Array#filter takes a callback -- which expresses what you want to keep instead of what you want to remove.

It first filters the given array with the native method, and then repeats it recursively for any children arrays, creating new objects with the newly filtered arrays that come back from such recursive calls:

function filterObjectArray(arr, filter) {
  return arr.filter(filter).map(obj => obj.children ? {
    ...obj,
    children: filterObjectArray(obj.children, filter)
  } : obj);
};

let data= [{"name": "Corporate","id": 1,"editMode": true,"children": [{"name": "Banner","id": 2,"parentId": 1,"editMode": true,"children": [{"name": "Division","id": 3,"parentId": 2,"editMode": false,"children": [{"name": "Region","id": 4,"editMode": true,"children": [{"name": "District","id": 5,"editMode": true,"children": [{"name": "Store","id": 6,"editMode": false,"children": []}]}]}]},{"name": "Banner1","id": 30,"editMode": true,"children": [{"name": "Banner11","id": 35,"editMode": true,"children": []}]},{"name": "Banner1","id": 31,"editMode": true,"children": []},{"name": "Banner1","id": 32,"editMode": true,"children": [{"name": "Banner11","id": 33,"editMode": true,"children": []},{"name": "Banner11","id": 34,"editMode": true,"children": []}]}]},{"name": "Corporate1","id": 36,"editMode": true,"children": [{"name": "Corporate11","id": 38,"editMode": true,"children": []},{"name": "Corporate11","id": 39,"editMode": true,"children": []}]},{"name": "Corporate1","id": 37,"editMode": true,"children": []}]}];

console.log(filterObjectArray(data, obj => obj?.id !== 30));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.