Creating a dictionary in JS files

Need help with your JScripts? Got questions concerning the DFNs? Come forward, step inside :)
Post Reply
dragon slayer
UOX3 Guru
Posts: 776
Joined: Thu Dec 21, 2006 7:37 am
Has thanked: 4 times
Been thanked: 26 times

Creating a dictionary in JS files

Post by dragon slayer »

Syntax: Key can be a string , integer. If you just write key1 or any number, it will treat as a string.
var dict = { key1 : value1 ,
             key2 : value2 ,
             ....
           };
Create empty dictionary
var dict = {};
Adding Key-Value Pairs in Dictionary
dict[new_key] = new_value;
or
If new_key is already present in the dictionary then the value of this will be updated to new_value.
dict.new_key = new_value;
Accessing Key-Value Pairs
var value = dict[key];
or
var value = dict.key;
Iterating the whole dictionary
for(var key in dict)
 {
   console.log(key + " : " + dict[key]);
}
These users thanked the author dragon slayer for the post:
Xuri
Post Reply