JavaScript Tutorial/MS JScript/Dictionary

Материал из Web эксперт
Перейти к: навигация, поиск

Dictionary

The Dictionary object is an associative array of items that can be of any type.

Each item is associated with a unique key that provides access to each item in the array.

The key is usually an integer or a string, but can be anything except an array.

Properties/Methods Description Count Returns the number of items in a collection or dictionary Item Sets or returns an item for a specified key Key Sets a key in a dictionary Add() Adds a key and item pair to dictionary Exists() Determines if specified key exists in dictionary Items() Returns array of all items in dictionary Keys() Returns array of all existing keys in dictionary Remove() Removes a key, item pair from dictionary RemoveAll() Removes all key, item pairs from a dictionary

Dictionary.Add()

Syntax



dictionaryobj.Add(key, item)


Dictionary.Count

The Count property contains the number of items in the dictionary.

This property is read only, so it cannot be used to change the size of the dictionary.



<html>
    <script language="JScript">
    <!--
    var fruits = new ActiveXObject("Scripting.Dictionary");
    fruits.Add("A","AA");
    fruits.Add("B","BB");
    fruits.Add("G","CC");
    fruits.Add("O","DD");
    document.write("There are ",fruits.Count," items in this dictionary.");
    -->
    </script>
    </html>


Dictionary.Exists()

Syntax



dictionaryobj.Exists(key)


Dictionary.Item()

Syntax



dictionaryobj.Item(key)
    dictionaryobj.Item(key) = newItem


Dictionary.Items()

Syntax



dictionaryobj.Items()


Dictionary.Key()

Syntax



dictionaryobj.Key(key) = newKey


Dictionary.Keys()

Syntax



dictionaryobj.Keys()


Dictionary.Remove()

Syntax



dictionaryobj.Remove(key)


Dictionary.RemoveAll()

The RemoveAll() method removes all key, item pairs from the dictionary. Nothing is returned from this method.



<html>
    <script language="JScript">
    <!--
    var fruits = new ActiveXObject("Scripting.Dictionary");

    fruits.Add("A","apple");
    fruits.Add("B","berry");
    fruits.Add("G","grape");
    fruits.Add("O","orange");
    fruits.RemoveAll();
    theArray = (new VBArray(fruits.Keys())).toArray();
    document.write("The array contains:<br>");
    for (i in theArray) {
      document.write("theArray[",i,"]=",theArray[i],"<br>");
    }
    -->
    </script>
</html>