I'm using TypeScript but this example is in plain JavaScript. I have a class rank
with a child-class master
. I need an ordered collection of unique masters. Here's what I have but it seems a little "kak":
function getMasters() {
var gms = [];
_.each(this.ranks, (r) => {
if (gms[r.master.id])
return;
gms[r.master.id] = new ExtendedRank(r.master.id r.master.code, r.master.name);
});
return _.without(gms, undefined).sort(this.sort);
}
This is the sort function (which I'm relatively happy with):
function sort(l: ExtendedRank, r: ExtendedRank) {
return l.sortString.localeCompare(r.sortString);
}