从父对象中过滤嵌套对象

时间:2019-04-19 21:53:12

标签: javascript arrays object

我有一个父对象,并在其下嵌套了一个子对象数组。每个对象都包含一个具有唯一值的id键。过滤器功能需要在父对象中搜索id,如果它不等于给定的id,则递归地在嵌套对象中搜索id,直到找到为止。找到具有给定键的对象后,请删除并返回更新的myObject

结构如下:

    let myObject = {
        key: 1,
        name: 'hello',
        children: [
            {
                key: 2,
                name: 'world',
                children: []
            },
            {
                key: 3,
                name: 'hope',
                children: [
                    {
                        key: 4,
                        name: 'you',
                        children: [{
                            key: 5,
                            name: 'are',
                            children: []
                        }]
                    },
                    {
                        key: 6,
                        name: 'having',
                        children: [{
                            key: 7,
                            name: 'fun',
                            children: []
                        }]
                    }
    
                ]
            }
        ]
    }
    
    
    let given = 4;

    if (myObject.key !== given) {
      myObject = searchChild(myObject, given)
    } else {
      myObject = {}
    }

    function searchChild(parent, given) {
        parent.children.map(child => {
          return child.children.filter(item => {
            if (item.key === given) return item;
            else if (item.key !== given 
                && child.children.length > 0 
                && child.children != undefined) { 
                  searchChild(child.children, given);
            }
          })
        })
      }

当前,我在运行递归函数时收到类型错误。

输出应看起来像树中键被更新为新顺序的地方:

{
        key: 1,
        name: 'hello',
        children: [
            {
                key: 2,
                name: 'world',
                children: []
            },
            {
                key: 3,
                name: 'hope',
                children: [
                    {
                        key: 4,
                        name: 'having',
                        children: [{
                            key: 5,
                            name: 'fun',
                            children: []
                        }]
                    }

                ]
            }
        ]
    }

2 个答案:

答案 0 :(得分:0)

您传递了child.children,但您必须传递child,而您已经通过函数中的children进行了迭代。

let myObject = {
        key: 1,
        name: 'hello',
        children: [
            {
                key: 2,
                name: 'world',
                children: []
            },
            {
                key: 3,
                name: 'hope',
                children: [
                    {
                        key: 4,
                        name: 'you',
                        children: [{
                            key: 5,
                            name: 'are',
                            children: []
                        }]
                    },
                    {
                        key: 6,
                        name: 'having',
                        children: [{
                            key: 7,
                            name: 'fun',
                            children: []
                        }]
                    }
    
                ]
            }
        ]
    }
    
    
    let given = 4;

    if (myObject.key !== given) {
      myObject = searchChild(myObject, given)
    } else {
      myObject = {}
    }

    function searchChild(parent, given) {
        if(parent && parent.children) {
parent.children.map(child => {
          return child.children.filter(item => {
            if (item.key === given) return item;
            else if (item.key !== given 
                && child.children.length > 0 
                && child.children != undefined) { 
                  searchChild(child, given);
            }
          })
        })
      }
}

答案 1 :(得分:0)

这是可以调用对象的函数

function searchInChild(parent,key){
      parent.children = parent.children.filter((c)=>{
        if(key == c.key ){
          result  = c;
          return false;
        }
        return true;
      });

      if(result == null){
        for(c in parent.children){
          searchInChild(parent.children[c],key);
        }
      }

    }

在这里,您只需传递searchInChild(myObject,key)并将结果设为全局变量即可。