Skip to content

Instantly share code, notes, and snippets.

@iendjinn
Created February 1, 2019 14:23
Show Gist options
  • Save iendjinn/ee192088804407f9cf51e261326ec866 to your computer and use it in GitHub Desktop.
Save iendjinn/ee192088804407f9cf51e261326ec866 to your computer and use it in GitHub Desktop.
class DataStore {
constructor(data, indexes) {
this.data = data;
this.indexes = indexes;
this.indexedData = {};
for (let i = 0; i < this.indexes.length; i++) {
const index = this.indexes[i];
this.indexedData[index] = [];
for (let j = 0; j < this.data.length; j++) {
const d = this.data[j];
if (!this.indexedData[d[index]]) {
this.indexedData[d[index]] = [];
}
this.indexedData[d[index]].push(d);
}
}
}
getByIndex(value) {
const matched = [];
for (let i = 0; i < this.indexes.length; i++) {
const index = this.indexes[i];
if (this.indexedData[index][value]) {
matched.push(this.indexedData[index][value]);
}
}
return matched;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment