Skip to content

Instantly share code, notes, and snippets.

@joemaffei
Last active November 22, 2024 21:00
Show Gist options
  • Save joemaffei/6593135297f400c69db9ab2a206d695b to your computer and use it in GitHub Desktop.
Save joemaffei/6593135297f400c69db9ab2a206d695b to your computer and use it in GitHub Desktop.
Indexed Object Array
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