简体   繁体   English

根据多个属性过滤 N 级嵌套对象数组

[英]Filter N level nested array of objects based upon multiple properties

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'}
          ]
        }
      ]
    }
  ];

Problem:问题:

I need to perform a keyword search on the question, and if a question is found at a node - 3 let's say then we need to return that node and all the parent nodes of that object.我需要对问题执行关键字搜索,如果在节点处找到问题 - 比方说 3,那么我们需要返回该节点和该 object 的所有父节点。

For example, if I search for 'hello there', the final tree should be:例如,如果我搜索“你好”,最终的树应该是:

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

We can have children or questions [] at any node.我们可以在任何节点有孩子或问题 []。

I am able to find the questions matching the search string, but I am not able to remove the unwanted nodes from the tree.我能够找到与搜索字符串匹配的问题,但无法从树中删除不需要的节点。 Here is the code for that:这是代码:

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)

This should work for you.这应该适合你。 The demo code is in stackblitz.演示代码在 stackblitz 中。 Check the result in the console.在控制台中查看结果。

Stackblitz demo Stackblitz 演示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM