You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arrayTreeFilter.js 645B

1 year ago
123456789101112131415161718192021222324
  1. /**
  2. * https://github.com/afc163/array-tree-filter
  3. */
  4. function arrayTreeFilter(data, filterFn, options) {
  5. options = options || {}
  6. options.childrenKeyName = options.childrenKeyName || 'children'
  7. let children = data || []
  8. const result = []
  9. let level = 0
  10. do {
  11. const foundItem = children.filter(function(item) {
  12. return filterFn(item, level)
  13. })[0]
  14. if (!foundItem) {
  15. break
  16. }
  17. result.push(foundItem)
  18. children = foundItem[options.childrenKeyName] || []
  19. level += 1
  20. } while (children.length > 0)
  21. return result
  22. }
  23. export default arrayTreeFilter