That is an interesting case that I hadn't considered before. I will definitely have to look into it while exploring JavaScript ecosystem more thoroughly!
In OOP it would probably be this:
class CatLookup { byName = new Map byId = new Map add(cat) { this.byName.set(cat.name, cat) this.byId.set(cat.id, cat) } addMany(cats) { for (const cat of cats) this.add(cat) } constructor(cats = []) { this.addMany(cats) } } const c = new CatLookup([ { name: "Aeris", id: 0x00, isFavourite: true }, { name: "Juri", id: 0x01 }, { name: "Dante", id: 0x03 }, { name: "Frankenstein", id: 0xff } ])
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
That is an interesting case that I hadn't considered before. I will definitely have to look into it while exploring JavaScript ecosystem more thoroughly!
In OOP it would probably be this: