UNPKG

util-helpers

Version:

一个基于业务场景的工具方法库

30 lines (27 loc) 890 B
import { isArray, forEach, isObject } from 'ut2'; function findTreeNodes(tree, predicate, childrenField) { if (childrenField === void 0) { childrenField = 'children'; } var stack = []; var nodes = []; if (isArray(tree)) { forEach(tree, function (item) { stack.push(item); while (stack.length) { var temp = stack.pop(); if (predicate(temp)) { nodes.push(temp); } if (isObject(temp)) { var childs = temp[childrenField]; if (isArray(childs) && childs.length > 0) { childs.forEach(function (c) { stack.push(c); }); } } } }); } return nodes; } export { findTreeNodes as default };