Dictionaries in JavaScript

Source: Internet
Author: User

1. Concept

A dictionary is a data structure stored in the form of a key-value pair, which is the same as the name and phone number in that phone book. To find a phone call first to find the name, and then find the phone number by name. The key here is the object to find, and the value is the result of finding it.

The object class in JavaScript is designed in the form of a dictionary. Using the attributes of the object class itself, we implement a dictionary class that makes the dictionary type more simple to use for the play.

The dictionary class is based on the array class, not the object class. As we'll mention later, we want to sort the keys in the dictionary, and in JavaScript we can't sort the properties of the objects by line. Everything in JavaScript is an object, and an array is an object.

Use the following method to define the dictionary class first:

function Dictionary () {    thisnew  Object ();}

First, define an Add method that takes two parameters, a key, and a value. The key is the index of the value under the dictionary, as follows:

function Add (key, value) {    this. Datastore[key] = value;} 

Next, define the Find method, which returns the value associated with the key as a parameter, with the following code:

function Find (Key) {    returnthis. Datastore[key];}

Deleting a key-value pair from a dictionary requires using a built-in function in JavaScript, delete, which is part of the object class and uses a reference to the key as a parameter that simultaneously deletes the value associated with it. The code is as follows:

function Remove (key) {    deletethis. Datastore[key];}

Finally, we want to be able to display all the key-value pairs in the dictionary, and here's a way to complete the task:

function ShowAll () {    for (var. datastore) {           This . Datastore[key]);        document.write (' <br> ');}    }

We can also define some helper methods that are useful in certain situations. For example, if you know the number of elements in the dictionary, then you can top a count method, as follows:

function count () {    var n = 0;      for (varin this. datastore) {        + +n;    }     return N;}

Many crossing and I would like to think, can not use the Length property, this is not possible, because when the type of the key is a string, the length property is no use, you can use the following code to test:

var New Array (); nums[0] = 1; nums[1] = 2//  display 2varnew  Array ();p book["David"] = 1;p book["Jennifer"] = 2//  display 0

Clear is another helper method, defined as follows:

function Clear () {    for (var. datastore) {         Delete  this. Datastore[key];}    }

The main purpose of a dictionary is to use key values, and we do not need the actual order in which the data is stored in the dictionary. Then a lot of people want to see an orderly dictionary. My practice is to first remove all key value pairs, put them in an array, then sort the array, and finally output the values in sorted order, as follows:

function sort () {    var keys = Array ();      for (varin this. datastore) {        keys.push (key);    }    Keys.sort ();      for (var i=0; i<keys.length; i++) {        this. datastore[keys[i]]);        document.write (' <br> ');}    }

2. Code implementation

Here's a look at all the problem codes above:

functionDictionary () { This. Add =add;  This. Datastore =NewObject ();  This. Find =find;  This. remove =remove;  This. ShowAll =ShowAll;  This. length =length;  This. Count =count;  This. Clear =Clear;  This. Sort =sort;}functionAdd (key, value) { This. Datastore[key] =value;}functionFind (key) {return  This. Datastore[key];}functionRemove (key) {Delete  This. Datastore[key];}functioncount () {varn = 0;  for(varKeyinch  This. Datastore) {        ++N; }    returnN;}functionShowAll () { for(varKeyinch  This. Datastore) {document.write (Key+ ' + ' + This. Datastore[key]); document.write (' <br> '); }}functionsort () {varKeys =Array ();  for(varKeyinch  This. Datastore)    {Keys.push (key);    } keys.sort ();  for(vari=0; i<keys.length; i++) {document.write (Keys[i]+ ' + ' + This. Datastore[keys[i]]); document.write (' <br> '); }}functionClear () { for(varKeyinch  This. Datastore) {        Delete  This. Datastore[key]; }}varPbook =NewDictionary ();p Book.add ("Raymond", "123");p Book.add ("David", "345");p Book.add ("Cynthia", "456");p Book.showall ();d Ocument.write (' After sort: ' + ' <br> ') Pbook.sort ();d Ocument.write ("Number of entries:" + pbook.count () + ' <br> ');d Ocument.write ("David ' s Extension:" + pbook.find ("David") + ' <br> ');p Book.showall ();p book.clear ();d Ocument.write ("Number of entries:" + pbook.count () + ' <br> ');

The output of the above code under the browser is as follows:

Dictionaries in JavaScript

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.