Created
February 1, 2019 14:23
-
-
Save iendjinn/ee192088804407f9cf51e261326ec866 to your computer and use it in GitHub Desktop.
This file contains 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 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