代码片段笔记

2022-07-12  本文已影响0人  ikonan

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);
};
上一篇下一篇

猜你喜欢

热点阅读