UNPKG

949 BJavaScriptView Raw
1var helperCreateTreeFunc = require('./helperCreateTreeFunc')
2var each = require('./each')
3
4function eachTreeItem (parent, obj, iterate, context, path, node, parseChildren, opts) {
5 var paths, nodes
6 each(obj, function (item, index) {
7 paths = path.concat(['' + index])
8 nodes = node.concat([item])
9 iterate.call(context, item, index, obj, paths, parent, nodes)
10 if (item && parseChildren) {
11 paths.push(parseChildren)
12 eachTreeItem(item, item[parseChildren], iterate, context, paths, nodes, parseChildren, opts)
13 }
14 })
15}
16
17/**
18 * 从树结构中遍历数据的键、值、路径
19 *
20 * @param {Object} obj 对象/数组
21 * @param {Function} iterate(item, index, items, path, parent, nodes) 回调
22 * @param {Object} options {children: 'children', mapChildren: 'children}
23 * @param {Object} context 上下文
24 */
25var eachTree = helperCreateTreeFunc(eachTreeItem)
26
27module.exports = eachTree