Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = function findChildren(root, items = [], opts = {}) {
- const { rootKey, foreignKey, withRoot } = opts
- const bypass = {}
- let children = [root]
- let nextItems = []
- recurse()
- if (!withRoot)
- children.shift()
- return children
- function recurse() {
- for (child of children) {
- if (bypass[child[rootKey]]) continue
- const foundChildren = items.filter(item => child[rootKey] === item[foreignKey])
- if (foundChildren.length) {
- children = children.concat(foundChildren)
- bypass[child[rootKey]] = true
- recurse()
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment