Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement a pseudo hash table by Javascript

Shulou Source: shulou.com Published: 2022-05-31 11:55:31 09月17日 Update

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!

Tags: Objects hashes methods properties number code content functions data learning practical deeper both ends examples interest at the same time characters strings practicality reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn Huawei NVidia Apple