The
The dictionary's properties and methods are as follows:
| Object | Description |
| length | Property returning the number of items in the dictionary |
| set(key, object) | Sets a key/value pair in the dictionary |
| remove(key) | Removes a key/value pair from the dictionary. (Trying to remove a non-existent key is not an error; it just silently does nothing.) |
| get(key) | Returns a value from the dictionary based on its key, or null if the key is not present |
| has(key) | Tests whether a key exists in the dictionary |
| is(keyOrArray) | Can take either a single key as a parameter, or an array of keys. Passing a single key is simply equivalent to |
| keys() | Returns all the keys in the dictionary as an array |
| values() | Returns all the values in the dictionary as an array |
| clear() | Empties the dictionary |
| forEach(fn) | Iterator. See description below. |
| each(fn) | Synonym of |
Note that the callback from
Framework.Instruments.forEach(function (key, instr) { console.log(key, instr.caption); });
This differs from the native Javascript
The dictionary's
// See if there is any object in the dictionary which has some particular value
var bHasValueWithProperty = false;
myDictionary.forEach(function (key, object) {
if (object.someProperty == someValue) {
// Set flag
bHasValueWithProperty = true;
// Exit from iteration, and avoid unnecessary further looping
return true;
}
});
Any non-null return value which terminates the iteration is passed back as the return value from
// See if there is any object in the dictionary which has some particular value.
// On return, matchingObject will contain either null or the first matching item.
var matchingObject = myDictionary.forEach(function (key, object) {
if (object.someProperty == someValue) {
// Exit from iteration, and avoid unnecessary further looping.
// The non-null value becomes the return value from the forEach().
return object;
}
});
Note: it is safe to modify the contents of the dictionary during
// Create a dictionary with 10 items
var x = new FXB.utils.Dictionary();
for (var i = 1; i <= 10; i++) x.set(i, i);
// Gets called 9 times because item 3 is removed on the first pass (where k = 1)
x.forEach (function (k, v) {
if (x.has(3)) x.remove(3); // Remove item 3 on first iteration
});