Skip to content

Instantly share code, notes, and snippets.

@saravanapriyanm
Created November 24, 2022 05:11
Show Gist options
  • Save saravanapriyanm/ad1357cb1b5d11ba2da3e646a885f87d to your computer and use it in GitHub Desktop.
Save saravanapriyanm/ad1357cb1b5d11ba2da3e646a885f87d to your computer and use it in GitHub Desktop.
Breadcrumbs
const breadcrumbs = [{
filterKey: 'Base',
filterValue: 'Base',
nextKey: 'Region'
}];
function addLevel({ nextKey, filterKey, filterValue }) {
const existing = breadcrumbs.find((breadcrumb) => breadcrumb.filterKey === filterKey);
if (existing) {
existing.filterValue = filterValue;
} else {
breadcrumbs.push({
filterKey,
filterValue,
nextKey
});
}
const index = breadcrumbs.findIndex((breadcrumb) => breadcrumb.filterKey === filterKey);
gotoLevel(index);
}
function gotoLevel(index) {
console.log(breadcrumbs[index].filterValue);
breadcrumbs.splice(index + 1);
}
function getText(){
return breadcrumbs.map((breadcrumb) => breadcrumb.filterValue).join(' > ');
}
addLevel({ filterKey: 'Region', filterValue: 'Karur', nextKey: 'District' });
console.log(getText());
addLevel({ filterKey: 'District', filterValue: 'Karur', nextKey: null });
console.log(getText());
// addLevel({ filterKey: 'Region', filterValue: 'Chennai' });
console.log(getText());
gotoLevel(0);
console.log(getText());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment