eachTree.js 922 B

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