A Dictionary (Dictionary)
A Dictionary is a data structure that stores data in key-value pairs, just as we do in our address book. If we want to find a phone number, we first look up the name of the phone number’s owner. The key here is what you’re looking for, in this case the name, and the value is the result of the search, which is the corresponding phone number.
In fact, the Object class in JavaScript is designed in the form of a Dictionary. Next, we will independently implement a Dictionary class with the help of the features of the Object class to make this kind of Dictionary Object more convenient to use.
Implementation of dictionary
The Dictionary class is based on the Array class.
Like the data structures we have seen before, dictionary classes should also have operations to add, delete, and empty, so we can define a dictionary class base data type, as shown in the following figure.
With this data type definition, our Dictionary class constructor definition is solved
Function Dictionary () {this.datastore = []; this.add = add; // Add element this.find = find; This. remove = remove; // Delete element this.count = count; // The number of elements in the dictionary this.showAll = showAll; // Display the dictionary element this.clear = clear; // Empty the dictionary}Copy the code
Add: Adds an element to the dictionary
As mentioned above, dictionaries store data as key-value pairs, so the add method takes two arguments, the key and the value, where the key represents its index in the dictionary, as follows
Function add(key, value){this.dataStore[key] = value; function add(key, value){this.dataStore[key] = value; }Copy the code
Yes, it’s that simple! Next, let’s look at the find method
Find: Finds elements in a dictionary
We are stored in key-value pairs, so we only need to pass in the key to find, we can logically get the corresponding value, which corresponds to the ARRAY in JS is also very simple;
Function find(key){return this.dataStore[key]; }Copy the code
With add and find, the next step is delete!
Remove: Removes an element from a dictionary
To delete an element in a dictionary, i.e. a key-value pair, we need to use a built-in function provided by JS: delete. This function is not unfamiliar to us, it can delete the key and its corresponding value at the same time, then remove method definition is very simple
Function remove(key){if(this.datastore [key]) delete this.datastore [key]; else return 'Not Found'; }Copy the code
In addition, we want to display all the key-value pairs in the dictionary, which is done by the showAll method.
ShowAll: Displays all key/value pairs in the dictionary
Function showAll () {for(var key in this.datastore){console.log(key + '->' + this.datastore [key]); }}Copy the code
Now that we’ve done the basic dictionary operations, let’s do a little test,
Var directory = new Dictionary(); // Add directory. Add ('Jack', '138****5505'); directory.add( 'Alice' , '156****6606'); directory.add( 'Tom' , '180****8808'); // display the dictionary directory.showall (); // Jack->138****5505 // Alice->156****6606 // Tom->180****8808 directory.remove( 'Tom' ); directory.showAll(); // Jack->138****5505 // Alice->156****6606Copy the code
When we defined it, we saw that there were two methods that were not implemented. One was count and the other was clear. Let’s implement it together.
Count: View the number of elements in the dictionary
This method can be useful sometimes, but it may not be implemented quite the way you think it is, so let’s see how it is implemented first, okay
Function count(){var n = 0; for ( var key in this.dataStore ){ ++n; } return n; }Copy the code
Why not use the length attribute? Isn’t that easy? If the key is a string, the length property of the array does not work. See the following example:
var nums = [ 0 , 1 , 2 ] ;
console.log(nums.length) // 3
var directory = [];
directory['Jack'] = '138****5505';
directory['Alice'] = '156****6606';
directory['Tom'] = '180****8808';
console.log(directory.length) // 0
Copy the code
Now I know another pit! Let’s implement the last clear method.
Clear: Clears the dictionary
Function clear(){for(var key in this.datastore){delete this.datastore [key]; }}Copy the code
Now that the dictionary is almost complete, let’s go ahead and test it with the code above
console.log(directory.count()); // 2 directory.clear(); console.log(directory.count()); / / 0Copy the code
Dictionaries are usually evaluated by keys, so we don’t care about the actual order in which the S data is stored in the dictionary, but we want to see that the contents of the dictionary are displayed in order, and that’s easy. We just need to tweak our showAll method a little bit.
ShowAll function showAll(){var sortKeys = object.keys (this.datastore).sort(); for( var key in sortKeys ){ console.log( sortKeys[key] + '->' + this.dataStore[sortKeys[key]] ); }}Copy the code
The only difference from our previous method is that once we get the keys, we sort them, and now let’s look at the output of the new method.
// re-print the dictionary directory.showall (); // Alice->156****6606 // Jack->138****5505 // Tom->180****8808Copy the code
Note that the showAll method returns a new array by sorting object.keys ().sort().
//sortKeys
["Alice", "Jack", "Tom"]
Copy the code
In this case, the array keys are 0, 1, and 2. Isn’t that much clearer?
At this point, we have a basic understanding of the dictionary some content, and we can use JS to implement a dictionary, isn’t it great! Next, everyone come on ~