DEV Community

Discussion on: Wormhole - Data Collection Cheat Sheet and Library in 4 Languages

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

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!

Collapse
 
qm3ster profile image
Mihail Malo • Edited

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 }
])