In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to implement a pseudo hash table in Javascript". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Javascript implements a pseudo hash table".
The code that implements the hash table in javascript:
1 function Hashtable ()
2 {
3 this._hash = {}
4 this._count = 0
5 this.add = function (key, value)
6 {
7 if (this._hash.hasOwnProperty (key)) return false
8 else {this._ hash [key] = value; this._count++; return true;}
9}
10 this.remove = function (key) {delete this._ hash [key]; this._count--;}
11 this.count = function () {return this._count;}
12 this.items = function (key) {if (this.contains (key)) return this._ hash [key];}
13 this.contains = function (key) {return this._hash.hasOwnProperty (key);}
14 this.clear = function () {this._hash = {}; this._count = 0;}
15}
It's easy to implement. We define a _ hash object in function, which has a property key, to which we can assign a value. The hasOwnProperty method is a method provided by javascript to return whether a property is contained in the specified object. At the same time, we also define a _ count object in the function, which is used to record the number of data in Hashtable, because we do not want to use a built-in loop to count every time we get the number of data in Hashtable, so the overhead will be less. As mentioned earlier, one of the basic features of the hashing algorithm is high efficiency. The delete statement is used to destroy an object in javascript.
Here are some examples of using this Hashtable:
1 var hashCompany = new Hashtable ()
two
3 / / add key-value pairs to Hashtable
4 function FillData (arr) {
5 hashCompany.clear ()
six
7 for (var I = 0; I < arr.length-1; iTunes +) {
8 if (arr [I]! = "") {
9 t = arr [I] .split ("`")
10 if (t.length > 2) {
11 if (! hashCompany.contains (t [0] .trim () {
12 hashCompany.add (t [0] .trim (), t [1])
13}
14}
15}
16}
17}
eighteen
19 / / traverse the Hashtable and take out the value
20 function GetDataFromHash () {
21 var s
22 if (hashCompany.count > 0) {
23 for (var i in hashCompany._hash) {
24 s + = I + "|"
25}
26}
twenty-seven
28 if (s.length > 0) {
29s = s.substring (0, s.length-2)
30}
thirty-one
32 return s
33}
The code is relatively simple, there is no more explanation here, in which a trim function is used, which is added below.
/ / regular expressions are used to remove the spaces at both ends of the string. Anonymous functions are used to extend the String object's method String.prototype.trim = function () {return this.replace (/ (^\ s *) | (\ shash $) / g, ""). At this point, I believe you have a deeper understanding of "how Javascript implements a pseudo hash table". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.