12345678910111213141516171819202122 |
- /**
- * 深度优先遍历算法, 遍历tree的每一项
- * @param {Object} {tree, path: 父节点的path, init: 是否是初始化}
- * @param {Callback}} cb 回调函数,参数为 node
- */
- export function depthFirstEach({ tree, parent = null }, cb) {
- if (!Array.isArray(tree)) {
- console.warn('The tree in the first argument to function depthFirstEach must be an array');
- return;
- }
- if (!tree || tree.length === 0) return;
- for (let node of tree) {
- const hasChildren = node.children && node.children.length > 0;
- if (cb) {
- const res = cb(node);
- if (res === 'break') return;
- }
- if (hasChildren) {
- depthFirstEach({ tree: node.children }, cb);
- }
- }
- }
|