代码片段笔记

findDeep (树节点查找)

const findDeep = (arr, callbackFn, childrenKey = 'children') => {
  if (!callbackFn) {
    return null;
  }

  return arr?.reduce((pre, cur) => {
    if (!!pre) return pre;
    if (callbackFn(cur)) return cur;

    const children = cur[childrenKey] || [];
    if (!!children.length) {
      return findDeep(children, callbackFn, childrenKey);
    }

    return pre;
  }, null);
};

你可能感兴趣的:(代码片段笔记)