UNPKG

1.26 kBJavaScriptView Raw
1var helperCreateTreeFunc = require('./helperCreateTreeFunc')
2
3function findTreeItem (parent, obj, iterate, context, path, node, parseChildren, opts) {
4 if (obj) {
5 var item, index, len, paths, nodes, match
6 for (index = 0, len = obj.length; index < len; index++) {
7 item = obj[index]
8 paths = path.concat(['' + index])
9 nodes = node.concat([item])
10 if (iterate.call(context, item, index, obj, paths, parent, nodes)) {
11 return { index: index, item: item, path: paths, items: obj, parent: parent, nodes: nodes }
12 }
13 if (parseChildren && item) {
14 match = findTreeItem(item, item[parseChildren], iterate, context, paths.concat([parseChildren]), nodes, parseChildren, opts)
15 if (match) {
16 return match
17 }
18 }
19 }
20 }
21}
22
23/**
24 * 从树结构中查找匹配第一条数据的键、值、路径
25 *
26 * @param {Object} obj 对象/数组
27 * @param {Function} iterate(item, index, items, path, parent, nodes) 回调
28 * @param {Object} options {children: 'children'}
29 * @param {Object} context 上下文
30 * @return {Object} { item, index, items, path, parent, nodes }
31 */
32var findTree = helperCreateTreeFunc(findTreeItem)
33
34module.exports = findTree