According to the search value, Filter gets the target tree structure.

question

data of Filter is required

const treeData = [
        {
          title: "0-0",
          key: "0-0",
          children: [
              {
                title: "0-0-0",
                key: "0-0-0",
                children: [
                  { title: "0-0-0-0", key: "0-0-0-0" },
                  { title: "0-0-0-1", key: "0-0-0-1" },
                  { title: "0-0-0-2", key: "0-0-0-2" },
                ],
              }, 
              {
                title: "0-0-1",
                key: "0-0-1",
                children: [
                  { title: "0-0-1-0", key: "0-0-1-0" },
                  { title: "0-0-1-1", key: "0-0-1-1" },
                  { title: "0-0-1-2", key: "0-0-1-2" },
                ],
              }, 
              {
                title: "0-0-2",
                key: "0-0-2",
              }
            ],
        }, 
        {
          title: "0-1",
          key: "0-1",
          children: [
            { title: "0-1-0-0", key: "0-1-0-0" },
            { title: "0-1-0-1", key: "0-1-0-1" },
            { title: "0-1-0-2", key: "0-1-0-2" },
          ],
        }, 
        {
          title: "0-2",
          key: "0-2",
        }
    ];

if the search value is "0-0-0-2", Filter is performed according to the value of title , and the result is as follows:

let newData = [
        {
          title: "0-0",
          key: "0-0",
          children: [
              {
                title: "0-0-0",
                key: "0-0-0",
                children: [
                  { title: "0-0-0-2", key: "0-0-0-2" },
                ],
              }
            ],
        }, 
    ];
< H2 > how to implement the above in JS code? < / H2 >
Nov.14,2021

const getByTitle = function(Data,title) {
    let Deep,T,F;
    for (F = Data.length;F;) {
        T = Data[--F]
        if (title === T.title) {
            return {title: T.title, key: T.key}//
        }
        if (T.children) {
            Deep = getByTitle(T.children, title)
            if (Deep) { //Deep 
                for (let i=0; i<T.children.length; iPP) {
                if (T.children[i].title === title) {//child
                    return {
                        key: T.key,
                        title: T.title,
                        children: [{
                            key: T.children[i].key,
                            title: T.children[i].title,
                        }]
                    }
                    break;
                } else {
                    if (T.children[i].children){
                        for (let j=0; j<T.children[i].children.length;jPP) {
                            if (T.children[i].children[j].title === title) {
                                return {
                                    key: T.key,
                                    title: T.title,
                                    children: [{
                                        key: T.children[i].key,
                                        title: T.children[i].title,
                                        children: [{
                                            key:T.children[i].children[j].title,
                                            title: title
                                        }]
                                    }]
                                }
                            }
                        }
                    }
                    
                }
                }
            }
        }
    }
}
 
Menu