Last active
November 22, 2024 21:00
-
-
Save joemaffei/6593135297f400c69db9ab2a206d695b to your computer and use it in GitHub Desktop.
Indexed Object Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IndexedObjectArray extends Array { | |
#map; | |
constructor(length) { | |
super(length); | |
} | |
toMap() { | |
return this.#map; | |
} | |
indexBy(key) { | |
this.#map = new Map(); | |
for (let obj of this) { | |
this.#map.set(obj[key], obj); | |
} | |
return this; | |
} | |
get(index) { | |
return this.#map.get(index); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment