【问题标题】:filter is not working for third child level (nested level)过滤器不适用于第三个子级别(嵌套级别)
【发布时间】:2019-11-25 22:34:20
【问题描述】:

我正在尝试过滤嵌套级别为 3 的数组。我必须在最后一级过滤此数组。

array = [{
    children: [{
        children: [{
            children: [],
            id: 2694,
            name: "Some Random data"
        }, {
            children: [],
            id: 2695,
            name: "Another Random Data"
        }],
        id: 2574,
        name: "Test data",
    }],
    id: 2530,
    name: "Main Test data"
}, {
    children: [{
        children: [{
            children: [],
            id: 2696,
            name: "Secondary test Data"
        }, {
            children: [],
            id: -1,
            name: "Random Text"
        }],
        id: 2575,
        name: "Another random Text"
    }],
    id: 2531,
    name: "New Data"
}]

这个功能我试过了

function(random){

let array3=[];
this.array.forEach(cap=>{
     let tempparent={...cap};
     let child1= tempparent.children.forEach(ch=>{
       let tempfeat={...ch};
       let tempchildren = tempfeat.children.filter(fe=>{
        if(fe.id!=random.id){
          return fe
        }
       });
     //  console.log(tempchildren)
       tempfeat.children = tempchildren;
     //  console.log(tempfeat.children)
     });
     console.log(child1)
     tempparent.children= child1;
     console.log(tempparent.children)
     nodes3.push(tempparent)
   })
   this.array= array3
   console.log(this.array);
}

我想使用 id 值在第三级过滤它。当 id 与匹配的对象匹配时,必须删除。

【问题讨论】:

  • 我想使用 id 值在第三级过滤它。当 id 匹配匹配的对象时必须删除
  • 请将想要的结果与错误一起添加到问题中,您会得到。
  • 或者我们可以通过 id 过滤完整的嵌套数组吗?如果 id 匹配匹配的对象必须删除
  • 我想要结果,因为只要 id 匹配,必须从给定数组中删除匹配的 id 对象

标签: javascript typescript


【解决方案1】:

您可以采取动态方法,将孩子从对象中取出,检查id,如果找到,则忽略该对象。

否则,通过递归调用获取子集并重建一个新对象并将其推送到结果集。

这种方法不会改变原始数据,而是返回所有新对象并适用于任意数量的级别。

function remove(array, id) {
    return array.reduce((r, { children, ...o }) => {
        if (o.id === id) return r;
        children = remove(children || [], id);
        if (children.length) o.children = children;
        r.push(o);
        return r;
    }, []);
}

var data = [{ children: [{ children: [{ children: [], id: 2694, name: "Some Random data" }, { children: [], id: 2695, name: "Another Random Data" }], id: 2574, name: "Test data", }], id: 2530, name: "Main Test data" }, { children: [{ children: [{ children: [], id: 2696, name: "Secondary test Data" }, { children: [], id: -1, name: "Random Text" }], id: 2575, name: "Another random Text" }], id: 2531, name: "New Data" }],
    result = remove(data, 2574);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 嗨,感谢您的回答..当我尝试在 Angular 6 中复制答案时,我在删除代码的第 4 行时遇到错误...说找不到名称删除...
  • 函数你接了吗?
  • 我根据我的要求做了一些更改。它的工作完美...感谢您的回答...
【解决方案2】:

你可以使用递归函数。

比如这样

function removeById(_id,arr){
    if(arr.id === _id){
    return true;
  }else{
    arr.children.forEach(currentItem => {
        if(getById(_id,currentItem)){
            arr.children = arr.children.filter(x=>x.id !== _id);
        }
    });
  }
}

并使用此功能

removeById(2694,array[0]);
removeById(2694,array[1]);

请检查example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2018-03-05
    • 2018-09-06
    • 2016-04-07
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多