Guest User

Untitled

a guest
Jan 20th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. module.exports = function findChildren(root, items = [], opts = {}) {
  2. const { rootKey, foreignKey, withRoot } = opts
  3. const bypass = {}
  4. let children = [root]
  5. let nextItems = []
  6. recurse()
  7. if (!withRoot)
  8. children.shift()
  9. return children
  10.  
  11. function recurse() {
  12. for (child of children) {
  13. if (bypass[child[rootKey]]) continue
  14. const foundChildren = items.filter(item => child[rootKey] === item[foreignKey])
  15. if (foundChildren.length) {
  16. children = children.concat(foundChildren)
  17. bypass[child[rootKey]] = true
  18. recurse()
  19. }
  20. }
  21. }
  22. }
Add Comment
Please, Sign In to add comment