Introduced in Firefox 5, this is now obsolete and has been removed in Firefox 40: use ES2015 Map
.
The Dict.jsm
JavaScript code module offers routines for managing dictionaries of key/value pairs. To use it, you first need to import the code module into your JavaScript scope:
Components.utils.import("resource://gre/modules/Dict.jsm");
You can create a new, empty dictionary by simply calling the Dict()
constructor:
var newDict = new Dict();
If you wish, you may also pass in an object literal of key/value pairs with which to initialize the dictionary:
var someObj = {}; var newDict = new Dict({key1: "foo", key2: someObj});
Note that values may be any JavaScript object type.
In Firefox 19 and above, you may also pass a JSON String to initialize the dictionary:
var someJSON = '{key1: "foo", key2: {}}'; var newDict = new Dict(someJSON);
Dict copy(); |
boolean del(String aKey); |
Object get( String aKey , [optional] Object aDefault); |
boolean has( String aKey ); |
Array listitems(); |
Array listkeys(); |
Array listvalues(); |
void set(String aKey , Object aValue); |
String toJSON(); |
String toString(); |
Attribute | Type | Description |
count |
Number |
The number of items in the dictionary. |
items |
Iterator |
Returns an iterator over all of the items in the dictionary; each item is returned as a pair (a two-element array) with the first element being the key and the second being the value. Note: The order in which items are returned is arbitrary, and may change without notice. In addition, if the dictionary changes during iteration, no guarantees are made as to what will happen.
|
keys |
Iterator |
Returns an iterator over all the keys in the dictionary. Note: The order in which items are returned is arbitrary, and may change without notice. In addition, if the dictionary changes during iteration, no guarantees are made as to what will happen.
|
values |
Iterator |
Returns an iterator over all the values in the dictionary. Note: The order in which items are returned is arbitrary, and may change without notice. In addition, if the dictionary changes during iteration, no guarantees are made as to what will happen.
|
Creates and returns a new dictionary object.
Dict Dict(); Dict Dict( Object initalKeysAndValues );
initialKeysAndValues
OptionalA newly created dictionary object implementing the methods described in this article.
Returns a shallow copy of the dictionary; that is, a copy of the dictionary including the items immediately included within the dictionary; however, any objects referenced by those top-level objects are not copied.
Dict copy();
None.
A new dictionary object containing the same top-level items as the original dictionary on which the copy()
method was called.
Deletes a key/value pair from the dictionary, given a key.
boolean del( String aKey );
aKey
true
if an entry was found and removed; false
if no match was found.
Returns the value corresponding to the specified key.
Object get( String
aKey
, [optional] Object aDefault );
aKey
aDefault
Optionalundefined
is returned for keys that aren't found.The value of the specified key, or undefined
if no matching key was found.
Determines whether or not the specified key exists in the dictionary.
boolean has( String
aKey
);
aKey
true
if the specified key is in the dictionary, otherwise false
.
Returns an array of the key/value pairs in the dictionary.
Array listitems();
None.
An array of the key/value pairs in the dictionary.
Returns an array of the keys in the dictionary.
Array listkeys();
None.
An array of the keys in the dictionary.
Returns an array of the values in the dictionary.
Array listvalues();
None.
An array of the values in the dictionary.
Sets the value for a given key, adding a new key to the dictionary if necessary.
void set( String
aKey
, Object aValue );
aKey
aValue
Returns a JSON String representation of the dictionary.
String toJSON();
None.
A JSON String representation of the dictionary's key/value pairs. This will be "{}" for an empty dictionary.
Returns a string representation of the dictionary.
String toString();
None.
A string representation of the dictionary's key/value pairs. This will be "{}" for an empty dictionary.
This example creates a new dictionary and adds some key/value pairs to it.
var myDict = new Dict(); myDict.set("name", "John Smith"); myDict.set("email", "jsmith@example.com"); myDict.set("phone", "650-555-1234");
You can then check to see if a given key exists:
if (myDict.has("email")) { /* an "email" key exists on the object */ }