Javascript tree search array

for the requirements on the front end, you need to write a search function to traverse the tree,

function search(nodes, keyword){
//  `const nodes`
}
const searchedNodes = search(nodes, "1-1-2-1"); 
//  "1-1-2-1"
/*
searchedNodes 
[
    {
    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"
        }
      ] }
    ]
  },
];

how should such a tree search be completed?

2018-06-26 Update

I wrote a version of DFS by myself last night. It doesn"t seem to work very well, but it works

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

probably wrote:


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"); 




the expected data and problem description you gave are ambiguous,

  children: [
            { value: "1-1-2-1-1" },
            { value: "1-1-2-1-2" }
          ]
{value: "1-1-2-1-2"} in

is also consistent with 1-1-2-1 .

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b33562-34236.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b33562-34236.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?