第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

javascript 樹(shù)狀搜尋 數(shù)組

javascript 樹(shù)狀搜尋 數(shù)組

前端上的需求,需要寫一個(gè)搜尋的 function 來(lái)遍歷這個(gè)樹(shù),function search(nodes, keyword){//預(yù)期輸入 如下方的 `const nodes`}const searchedNodes = search(nodes, "1-1-2-1"); // 預(yù)期要輸出包含 "1-1-2-1"的所有節(jié)點(diǎn)以及該節(jié)點(diǎn)的父節(jié)點(diǎn)/*searchedNodes 應(yīng)該要相等于[    {    value: "1-1",    children: [      { value: "1-1-2", children:[        {          value: "1-1-2-1",          children: [            { value: "1-1-2-1-1" }          ]        }      ] }    ]  }]*/const nodes = [  {    value: "1-1",    children: [      { value: "1-1-1"},      { value: "1-1-2", children:[        {          value: "1-1-2-1",          children: [            { value: "1-1-2-1-1" },            { value: "1-1-2-1-2" }          ]        },        {          value: "1-1-2-2"        }      ] }    ]  },  {    value: "1-2",    children: [      { value: "1-2-1"},      { value: "1-2-2", children:[        {          value: "1-2-2-1",          children: [            { value: "1-2-2-1-1" },            { value: "1-2-2-1-2" }          ]        },        {          value: "1-2-2-2"        }      ] }    ]  },];請(qǐng)問(wèn)這樣的樹(shù)搜尋應(yīng)該要怎麼完成呢?昨晚自己折騰了一會(huì) 寫了一個(gè) DFS 的版本 效能似乎不太好 但堪用const search = (nodes, keyword) => {    let newNodes = [];    for (let n of nodes) {      if (n.children) {        const nextNodes = this.keywordFilter(n.children, keyword);        if (nextNodes.length > 0) {          n.children = nextNodes;        } else if (n.label.toLowerCase().includes(keyword.toLowerCase())) {          n.children = nextNodes.length > 0 ? nextNodes : [];        }        if (          nextNodes.length > 0 ||          n.label.toLowerCase().includes(keyword.toLowerCase())        ) {                  newNodes.push(n);        }      } else {        if (n.label.toLowerCase().includes(keyword.toLowerCase())) {          newNodes.push(n);        }      }    }    return newNodes;  };
查看完整描述

2 回答

?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

大概寫了一下:



function compare(value, keyword){

    if(value === keyword){

        return 2;

    }

    if(new RegExp('^' + value).test(keyword)){

        return 1;

    }

    return 0;

}


function walk(node, keyword){


    let isFind = false;

    const path = [];

    let children = null;


    function dfs(node, keyword){

        if(compare(node.value, keyword) === 1){

            path.push(node.value);

            node.children && node.children.forEach(childNode => {

                dfs(childNode, keyword);

            });

        }

        if(compare(node.value, keyword) === 2) {

            node.children && (children = node.children);

            isFind = true;

        }

    }


    dfs(node, keyword);


    return {

        isFind,

        path,

        children

    }

}


function generateObj(pathArr, keyword, children){

    const resObj = {};

    let tempObj = resObj;

    pathArr.forEach(path => {

        tempObj.value = path;

        tempObj.children = [{}];

        tempObj = tempObj.children[0];

    });

    tempObj.value = keyword;

    children && (tempObj.children = children);

    return resObj;

}


function search(nodes, keyword){

    const searchedNodes = [];

    nodes.forEach(node => {

        const { isFind = false, path, children = null } = walk(node, keyword);

        if(isFind){

            searchedNodes.push(generateObj(path, keyword, children));

        }

    });

    return searchedNodes;

}


const nodes = [

  {

    value: "1-1",

    children: [

      { value: "1-1-1"},

      { value: "1-1-2", children:[

        {

          value: "1-1-2-1",

          children: [

            { value: "1-1-2-1-1" },

            { value: "1-1-2-1-2" }

          ]

        },

        {

          value: "1-1-2-2"

        }

      ] }

    ]

  },

  {

    value: "1-2",

    children: [

      { value: "1-2-1"},

      { value: "1-2-2", children:[

        {

          value: "1-2-2-1",

          children: [

            { value: "1-2-2-1-1" },

            { value: "1-2-2-1-2" }

          ]

        },

        {

          value: "1-2-2-2"

        }

      ] }

    ]

  },

];


const searchedNodes = search(nodes, "1-1-2-1"); 


查看完整回答
反對(duì) 回復(fù) 2019-04-09
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

你給的期望數(shù)據(jù)和 問(wèn)題描述有歧義啊,


  children: [

            { value: "1-1-2-1-1" },

            { value: "1-1-2-1-2" }

          ]

中的{ value: "1-1-2-1-2" }也是符合1-1-2-1的啊


查看完整回答
反對(duì) 回復(fù) 2019-04-09
  • 2 回答
  • 0 關(guān)注
  • 435 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)