Created
August 15, 2015 20:18
-
-
Save nobitagit/302ce8e0ae03df48e966 to your computer and use it in GitHub Desktop.
hashtable in js
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
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