Skip to content

Instantly share code, notes, and snippets.

@davidchase
Last active June 21, 2018 02:44
Show Gist options
  • Save davidchase/59b1458878f357f90ca99d01dea10a42 to your computer and use it in GitHub Desktop.
Save davidchase/59b1458878f357f90ca99d01dea10a42 to your computer and use it in GitHub Desktop.
Object+Array Simple Multi-Map
class MultiMap {
constructor(container = Array) {
this.map = {};
this.Container = container;
}
set(k, v) {
let container = this.get(k);
if (!container) {
container = new this.Container();
this.map[k] = container.concat(v)
}
container.push(v)
return this;
}
get(k) {
return this.map[k];
}
has(k) {
return this.map.hasOwnProperty(k);
}
values() {
return Object.values(this.map);
}
keys() {
return Object.keys(this.map);
}
entries() {
return Object.entries(this.map);
}
inspect() {
return this.map;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment