【问题标题】:Filter N level nested array of objects based upon multiple properties根据多个属性过滤 N 级嵌套对象数组
【发布时间】:2020-11-07 02:18:33
【问题描述】:
data = [
    {
      name: "Parent Level 1",
      questions: [
        {
          name: "question 1"
        }
      ],
      children: [
        {
          name: "Child 1 - P1",
          questions: [
            {
              name: "ability to code"
            },
            {
              name: "ability to do something"
            }
          ],
          children: [
            {
              name: "Child -2 P1",
              questions: [
                {
                  name: "figure out"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      name : 'Parent Level 2',
      questions : [
        {name : 'question 1 P-2'}
      ]
    },
    {
      name : 'Parent Level 3',
      children: [
        {
          name : 'Child Level -1 P-3',
          children: [
          {
             name : 'Child Level 2- P-3',
             questions : [
              {
       name : 'Question level 2
              }
             ]
           }
          ]
          questions: [
            {name : 'hello there'}
          ]
        }
      ]
    }
  ];

问题:

我需要对问题执行关键字搜索,如果在某个节点找到问题 - 假设是 3,那么我们需要返回该节点以及该对象的所有父节点。

例如,如果我搜索“你好”,最终的树应该是:

[
    {
      name : 'Parent Level 3',
      children: [
        {
          name : 'Child Level -1 P-3',
          children: [
          {
             name : 'Child Level 2- P-3',
             questions : []
           }
          ]
          questions: [
            {name : 'hello there'}
          ]
        }
      ]
    }
  ];

我们可以在任何节点有子节点或问题 []。

我能够找到与搜索字符串匹配的问题,但我无法从树中删除不需要的节点。这是代码:

searchNode (data) {
    for (let d of data) {
      this.search(d)
    }
 }

 search(data) {
    let search = 'ability'
    if(!!data.questions && data.questions.length > 0) {
      data.questions = data.questions.filter((question) => {
        return question.name.includes(search)
      })
    }
    if(data.children && data.children.length > 0) {
      searchNode(data.children)
    }
  }

search(data)

【问题讨论】:

    标签: javascript angular lodash


    【解决方案1】:

    这应该适合你。演示代码在 stackblitz 中。在控制台中检查结果。

    Stackblitz demo

    searchString = 'hello';
    filteredData = [];
    
    ngOnInit(): void {
        this.filteredData = [];
        this.data.forEach(node => {
            if (this.checkQtn(node)) {
                this.filteredData.push(node);
            }
        });
        console.log(this.filteredData);
    }
    
    checkQtn(node): Boolean {
        let response: Boolean = false;
        if (node.questions) {
            let qtns = [];
            qtns = node.questions;
            qtns.forEach(el => {
                const eachQtn: string = el.name;
                if (eachQtn.includes(this.searchString)) {
                    response = true;
                }
            });
        }
        if (!response && node.children) {
            for (let i = 0; i < node.children.length && !response; i++) {
                response = this.checkQtn(node.children[i]);
            }
        }
        return response;
    }
    

    【讨论】:

    • 这不会过滤子数组,例如,如果我在上面的示例中搜索“问题 1”,它应该只返回没有子数组的第一个节点。
    • 在您在问题中给出的示例中,预期的输出节点有父节点和子节点。只有当您正确起草问题时,我们才能为您提供帮助。如果您使用预期输出更新问题,我可以帮助您。
    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 2020-03-11
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2018-07-06
    相关资源
    最近更新 更多