Skip to content

Instantly share code, notes, and snippets.

@southbite
Created September 1, 2021 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save southbite/b6b40203012a455c375650ee5c97bea0 to your computer and use it in GitHub Desktop.
Save southbite/b6b40203012a455c375650ee5c97bea0 to your computer and use it in GitHub Desktop.
who'se your daddy algorithm, furry braaas funny PaaS
let organisations = [
{ id: 1, name: "grandaddy", parentId: null },
{ id: 2, name: "daddy", parentId: 1 },
{ id: 3, name: "sister", parentId: 2 },
{ id: 4, name: "brother", parentId: 2 }
]
const myChildId = 4;
function findChild(id) {
return organisations.find(org => {
return org.id === id;
});
}
let currentChild = findChild(myChildId);
const breadCrumb = [];
while(currentChild.parentId != null) {
breadCrumb.unshift(currentChild);
currentChild = findChild(currentChild.parentId);
}
breadCrumb.unshift(currentChild);
console.log(breadCrumb);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment