Skip to content

Instantly share code, notes, and snippets.

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 ZackDeRose/69331e73714bdabf4f1990ebf71962f6 to your computer and use it in GitHub Desktop.
Save ZackDeRose/69331e73714bdabf4f1990ebf71962f6 to your computer and use it in GitHub Desktop.
Snippet of creating superlatives dictionary for "Angular CDK Tables"
ngOnInit() {
this.heroes$.subscribe(changedHeroData => {
this.tableDataSource$.next(Object.values(changedHeroData));
const superlatives = {
'highest-attack': null,
'lowest-attack': null,
'highest-defense': null,
'lowest-defense': null,
'highest-speed': null,
'lowest-speed': null,
'highest-healing': null,
'lowest-healing': null,
'highest-recovery': null,
'lowest-recovery': null,
'highest-health': null,
'lowest-health': null
};
Object.values(changedHeroData).forEach(hero => {
Object.keys(hero).forEach(key => {
if (key === 'name' || key === 'types') { return; }
const highest = `highest-${key}`;
if (!superlatives[highest] || hero[key] > changedHeroData[superlatives[highest]][key]) {
superlatives[highest] = hero.name;
}
const lowest = `lowest-${key}`;
if (!superlatives[lowest] || hero[key] < changedHeroData[superlatives[lowest]][key]) {
superlatives[lowest] = hero.name;
}
});
});
this.superlatives$.next(superlatives);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment