js树形结构中搜索某一项数据

在树形结构中搜索 checkItems值为 某某某的数据,返回的是子数据。

const searchTree = (tree, checkItems) => {
    let res = '';
    let state = false;
    const readTree = (_tree, _checkItems) => {
      if (state) return;
      for (let i = 0; i < _tree.length; i++) {
        if (_tree[i].checkItems === _checkItems) {
          state = true;
          res = _tree[i];
        } else {
          _tree[i].children !== null && readTree(_tree[i].children, _checkItems);
        }
      }
    };
    readTree(tree, checkItems);
    return res;
};
posted @ 2023-04-24 17:59  ZerlinM  阅读(472)  评论(0编辑  收藏  举报