Skip to content

Instantly share code, notes, and snippets.

@blurymind
Last active December 6, 2019 10:06
Show Gist options
  • Save blurymind/b9977d0920334ba323cf8f5e3b9a68dc to your computer and use it in GitHub Desktop.
Save blurymind/b9977d0920334ba323cf8f5e3b9a68dc to your computer and use it in GitHub Desktop.
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