1
 <script>    var itemsTemp= [
             { id: 0, text: 'Andy' },
             {
               id: 1, text: 'Harry',
               children: [
                 { id: 2, text: 'David' }
               ]
             },
             { id: 3, text: 'Lisa' },
             { id: 4, text: 'Mona' },
             { id: 5, text: 'Ron' },
             { id: 6, text: 'Joe' }
           ];
 
   var items  = itemsTemp;
 
         var filtered = items.filter(function(item) { 
             return item.id !== 3;  
         });
 
         console.log(filtered);
 
 </script>

in this way, I can only remove the parent but how can I delete the child object? please help me to fix this

2
  • 2
    If you want to remove a member from an object. You can use delete. Commented Jan 7, 2021 at 2:36
  • Do you need to be able to remove from both parent list and child list or just child list? Commented Jan 7, 2021 at 2:40

3 Answers 3

1

Since you want to filter children, you can use .reduce() to perform a mapping and filtering of your array. When you reach an object which has a children property, you can recursively call your function to then perform the mapping/filtering on the child array .reduce() array like so:

const items = [{ id: 0, text: 'Andy' }, { id: 1, text: 'Harry', children: [{ id: 2, text: 'David' }] }, { id: 3, text: 'Lisa' }, { id: 4, text: 'Mona' }, { id: 5, text: 'Ron' }, { id: 6, text: 'Joe' } ];

const filterItems = (items, fn) => items.reduce((acc, item) => {
  if(item.children)
    return [...acc, ...filterItems(item.children, fn)];
  else if(fn(item))
     return [...acc, item];
  return acc;
}, []);

const filtered = filterItems(items, item => item.id !== 2);
console.log(filtered);

If you don't want to remove the item from the parent list, and only from the child list, then you push an update object instead:

const items = [{ id: 0, text: 'Andy' }, { id: 1, text: 'Harry', children: [{ id: 2, text: 'David' }] }, { id: 3, text: 'Lisa' }, { id: 4, text: 'Mona' }, { id: 5, text: 'Ron' }, { id: 6, text: 'Joe' } ];

const toRemoveId = 2;
const filterItems = (items, fn) => items.reduce((acc, item) => {
  if(item.children)
    return [...acc, {...item, children: filterItems(item.children, fn)}];
  else if(fn(item))
     return [...acc, item];
  return acc;
}, []);

const filtered = filterItems(items, item => item.id !== 2);
console.log(filtered);

This will work for arbitrary object depths.

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

Comments

1

I just wrote the filterById function I think it works for your case

var itemsTemp = [
  { id: 0, text: "Andy" },
  {
    id: 1,
    text: "Harry",
    children: [{ id: 2, text: "David" }],
  },
  { id: 3, text: "Lisa" },
  { id: 4, text: "Mona" },
  { id: 5, text: "Ron" },
  { id: 6, text: "Joe" },
];

var items = itemsTemp;

const filterById = (items, id) => {
    return items.reduce((accumulator, currentValue) => {
        if(currentValue.children){
          const newCurrentValue = filterById(currentValue.children, id)
          currentValue = {...currentValue, children: newCurrentValue}
        }
        if(currentValue.id !== id){
          return [...accumulator, currentValue]
        }
        return accumulator
    },[])
}

console.log(filterById(itemsTemp,2));
console.log(itemsTemp)

1 Comment

Yes I just update the way it won't modify the original object. As filterById we can dynamic the id better than just filter by fixed ID.
0

I think you can do like this.

   var itemsTemp= [
             { id: 0, text: 'Andy' },
             {
               id: 1, text: 'Harry',
               children: [
                 { id: 2, text: 'David' }
               ]
             },
             { id: 3, text: 'Lisa' },
             { id: 4, text: 'Mona' },
             { id: 5, text: 'Ron' },
             { id: 6, text: 'Joe' }
           ]; 
        var items  = itemsTemp; 
        var filtered = items.filter(function(item) {             
             childrens=item.children;            
             if(childrens)
             {
              filteredchildren = childrens.filter(children=>children.id!==2);
              item.children=filteredchildren;
             }
            
             return item.id !== 2;
         });
         console.log(filtered);

2 Comments

Working good thank you. How can i remove if have a child componet inside the child componet var itemsTemp= [ { id: 0, text: 'Andy' }, { id: 1, text: 'Harry', children: [ { id: 2, text: 'David', children: [ { id: 20, text: 'David20' }, ] }, ] }, { id: 3, text: 'Lisa' }, { id: 4, text: 'Mona' }, { id: 5, text: 'Ron' }, { id: 6, text: 'Joe' } ];
@BiswanathPrasadSingh btw, this solution will only work if your objects are nested two levels deep. This wouldn't handle the case for when { id: 2, text: 'David' } has a children array and holds an object with the id you want to remove

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.