filterTree.js 658 B

123456789101112131415161718192021222324
  1. var eachTree = require('./eachTree')
  2. /**
  3. * 从树结构中根据回调过滤数据
  4. *
  5. * @param {Object} obj 对象/数组
  6. * @param {Function} iterate(item, index, items, path, parent) 回调
  7. * @param {Object} options {children: 'children'}
  8. * @param {Object} context 上下文
  9. * @return {Array}
  10. */
  11. function filterTree (obj, iterate, options, context) {
  12. var result = []
  13. if (obj && iterate) {
  14. eachTree(obj, function (item, index, items, path, parent, nodes) {
  15. if (iterate.call(context, item, index, items, path, parent, nodes)) {
  16. result.push(item)
  17. }
  18. }, options)
  19. }
  20. return result
  21. }
  22. module.exports = filterTree