Skip to content

Instantly share code, notes, and snippets.

@nobitagit
Created August 15, 2015 20:18
Show Gist options
  • Save nobitagit/302ce8e0ae03df48e966 to your computer and use it in GitHub Desktop.
Save nobitagit/302ce8e0ae03df48e966 to your computer and use it in GitHub Desktop.
hashtable in js
var HashTable = function (key) {
this.store = {};
this.key = key;
};
HashTable.prototype = {
getItem : function (key) {
return this.store[key];
},
pushItem : function (item) {
this.store[item[this.key]] = item;
},
get : function (){
return this.store;
},
add : function (data) {
var _this = this;
if (Array.isArray(data)) {
data.forEach(function (item) {
_this.pushItem(item);
});
} else {
this.pushItem(data);
}
return this.store;
},
destroy : function () {
this.store = {};
return this.store;
}
};
var booksHash = new HashTable('id');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment