【问题标题】:How to remove child object from parent object list javascript如何从父对象列表中删除子对象javascript
【发布时间】:2021-04-12 19:07:22
【问题描述】:
 <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>

这样,我只能删除父对象,但如何删除子对象?请帮我解决这个问题

【问题讨论】:

  • 如果你想从一个对象中删除一个成员。你可以使用delete
  • 您需要能够从父列表和子列表中删除还是仅从子列表中删除?

标签: javascript json ecmascript-6


【解决方案1】:

由于您想过滤孩子,您可以使用.reduce() 来执行您的数组的映射和过滤。当您到达具有children 属性的对象时,您可以递归调用您的函数,然后对子数组.reduce() 数组执行映射/过滤,如下所示:

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);

如果您不想从父列表中删除项目,而只想从子列表中删除,则改为推送更新对象:

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);

这适用于任意物体深度。

【讨论】:

    【解决方案2】:

    我刚刚编写了 filterById 函数,我认为它适用于您的情况

    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)

    【讨论】:

    • 是的,我只是更新了它不会修改原始对象的方式。与 filterById 一样,我们可以更好地动态化 id,而不是仅通过固定 ID 进行过滤。
    【解决方案3】:

    我认为你可以这样做。

       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);

    【讨论】:

    • 工作得很好,谢谢。如果子组件 var itemsTemp= [ { id: 0, text: 'Andy' }, { id: 1, text: 'Harry', children: [ { id: 2, text: '大卫',孩子:[{ id:20,文本:'David20'},]},]},{ id:3,文本:'Lisa'},{ id:4,文本:'Mona'},{ id: 5, text: 'Ron' }, { id: 6, text: 'Joe' } ];
    • @BiswanathPrasadSingh 顺便说一句,此解决方案仅在您的对象嵌套两层深度时才有效。当{ id: 2, text: 'David' } 有一个children 数组并持有一个带有您要删除的 id 的对象时,这将无法处理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2019-11-25
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多