Last active
December 6, 2019 10:06
-
-
Save blurymind/b9977d0920334ba323cf8f5e3b9a68dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getBottomChildren = (scanObject: any, limit: number | null = null, collectedChildren: Array<any> = []) => { | |
if (limit && collectedChildren.length === limit) return collectedChildren; | |
if (scanObject.children === null) collectedChildren.push(scanObject); | |
else scanObject.children.forEach(child => getBottomChildren(child, limit, collectedChildren)); | |
return collectedChildren; | |
}; | |
export const collectChildrenWithKey = ( | |
scanObject: any, | |
key: string, | |
limit: number | null = null, | |
collected: Array<DevicesType> = [], | |
) => { | |
if (limit && collected.length === limit) return collected; | |
if (scanObject[key]) { | |
collected.push(scanObject); | |
} else if (scanObject.children) { | |
scanObject.children.forEach(child => collectChildrenWithKey(child, key, limit, collected)); | |
} | |
return collected; | |
}; | |
const findObjectWithKey = (object, key) => { | |
if (key in object) { | |
return object; | |
} | |
if (object.children) { | |
return object.children.find(child => findObjectWithKey(child, key)); | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment