Javascript pseudo hash table

Source: Internet
Author: User
Tags hasownproperty

People who know the data structure should have heard of the hash table data structure. It is a typical non-linear structure that uses key-value pairs to store and retrieve data, it is also called the hash or collection method. In a general linear table structure, the relative positions of data are random, that is, there is no definite relationship between the data and the keywords used for retrieval. A series of comparisons are often required for data retrieval, finally, we find the data to be retrieved. This method is usually based on the cyclic comparison mechanism, which saves space at the cost of time and achieves the data storage and retrieval functions. A hash table uses key-value pairs for data storage, and establishes a one-to-one correspondence between the data storage location and Its keywords, therefore, each keyword corresponds to a unique storage location in the structure. Therefore, when retrieving data, you only need to quickly locate the data to be searched based on the corresponding relationship.

In fact, we usually do not care about how data is stored, but whether it is convenient for us to use data, such as sorting, searching, and replacing data, because this operation runs throughout the entire application, the efficiency requirement is very high. General advanced languages, such as C and C variants, provide data structures for storing hash tables. However, in weak languages such as JavaScript, it does not directly provide such a structure similar to a hash table, but we can start from an array and create a proprietary hash table data structure in script language based on the hash table principle.

We know that in the data, the corresponding data can be located directly through the subscript, that is, the space used to store the data. This attribute of the array determines that it can be used to implement the hash algorithm, however, in C language, the subscript of an array can only be an integer starting from 0, rather than other types of data. However, in Javascript, we can use the concept of "object, simulate the hash algorithm based on the array features. In JavaScript, an object is actually a set of attributes or methods. Therefore, we can construct a hashtable object, which has two attributes: Key and value, write your own code to simulate a complete hash table. The following code implements a hash table in javascript:

 

function Hashtable()  {      this._hash = {};      this._count = 0;      this.add = function(key, value)       {         if (this._hash.hasOwnProperty(key)) return false;         else { this._hash[key] = value; this._count++; return true; }      }       this.remove = function(key) { delete this._hash[key]; this._count--; }       this.count = function() { return this._count; }       this.items = function(key) { if (this.contains(key)) return this._hash[key]; }       this.contains = function(key) { return this._hash.hasOwnProperty(key); }       this.clear = function() { this._hash = {}; this._count = 0; } }

 

It is easy to implement. We define a _ Hash object in the function. This object has an attribute key. We can assign values to this attribute. The hasownproperty method is a method provided by JavaScript, returns whether the specified object contains an attribute. At the same time, we define a _ count object in this function to record the number of data in hashtable, because we don't want to count every time we get the number of data in hashtable through a built-in loop, this will reduce the overhead. As mentioned above, a basic feature of the hash algorithm is high efficiency. The delete statement is used in JavaScript to destroy an object.

The following are some examples of using this hashtable:

VaR hashcompany = new hashtable (); // Add the key-Value Pair function filldata (ARR) {hashcompany to hashtable. clear (); For (VAR I = 0; I <arr. length-1; I ++) {If (Arr! = "") {T = arr. Split ("'"); If (T. length> 2) {If (! Hashcompany. contains (T [0]. trim () {hashcompany. add (T [0]. trim (), t [1]) ;}}}// traverse hashtable and retrieve the value function getdatafromhash () {var s; If (hashcompany. count> 0) {for (var I in hashcompany. _ Hash) {S + = I + "|" ;}} if (S. length> 0) {S = S. substring (0, S. length-2);} return s ;}

The code is relatively simple, so I will not add more details here. Here I use a trim function and I will add it below.

// Use a regular expression to remove spaces at both ends of the string. The anonymous function is used to extend the string object's method string. prototype. trim = function () {return this. replace (/(^ \ s *) | (\ s * $)/g ,"");}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.