UNPKG

util-helpers

Version:

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

32 lines (28 loc) 892 B
'use strict'; var ut2 = require('ut2'); function findTreeNodes(tree, predicate, childrenField) { if (childrenField === void 0) { childrenField = 'children'; } var stack = []; var nodes = []; if (ut2.isArray(tree)) { ut2.forEach(tree, function (item) { stack.push(item); while (stack.length) { var temp = stack.pop(); if (predicate(temp)) { nodes.push(temp); } if (ut2.isObject(temp)) { var childs = temp[childrenField]; if (ut2.isArray(childs) && childs.length > 0) { childs.forEach(function (c) { stack.push(c); }); } } } }); } return nodes; } module.exports = findTreeNodes;