-
-
Removes all previously stored key-value pairs from the Dictionary.
Example
function setup() {
let myDictionary = createStringDict('p5', 'js');
print(myDictionary.hasKey('p5')); // prints 'true'
myDictionary.clear();
print(myDictionary.hasKey('p5')); // prints 'false'
}
-
-
Creates a new key-value pair in the Dictionary.
Parameters:
Name |
Type |
Description |
key |
Number
|
String
|
|
value |
Number
|
String
|
|
Example
function setup() {
let myDictionary = createStringDict('p5', 'js');
myDictionary.create('happy', 'coding');
myDictionary.print();
// above logs "key: p5 - value: js, key: happy - value: coding" to console
}
-
-
Returns the value stored at the given key.
Parameters:
Name |
Type |
Description |
the |
Number
|
String
|
key you want to access |
Returns:
Number
|
String
- the value stored at that key
Example
function setup() {
let myDictionary = createStringDict('p5', 'js');
let myValue = myDictionary.get('p5');
print(myValue === 'js'); // logs true to console
}
-
-
Returns true if the given key exists in the Dictionary,
otherwise returns false.
Parameters:
Name |
Type |
Description |
key |
Number
|
String
|
that you want to look up |
Returns:
Boolean
- whether that key exists in Dictionary
Example
function setup() {
let myDictionary = createStringDict('p5', 'js');
print(myDictionary.hasKey('p5')); // logs true to console
}
-
-
Logs the set of items currently stored in the Dictionary to the console.
Example
function setup() {
let myDictionary = createStringDict('p5', 'js');
myDictionary.create('happy', 'coding');
myDictionary.print();
// above logs "key: p5 - value: js, key: happy - value: coding" to console
}
-
-
Removes the key-value pair stored at the given key from the Dictionary.
Parameters:
Name |
Type |
Description |
key |
Number
|
String
|
for the pair to remove |
Example
function setup() {
let myDictionary = createStringDict('p5', 'js');
myDictionary.create('happy', 'coding');
myDictionary.print();
// above logs "key: p5 - value: js, key: happy - value: coding" to console
myDictionary.remove('p5');
myDictionary.print();
// above logs "key: happy value: coding" to console
}
-
-
Converts the Dictionary into a JSON file for local download.
Example
function setup() {
createCanvas(100, 100);
background(200);
text('click here to save', 10, 10, 70, 80);
}
function mousePressed() {
if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
createStringDict({
john: 1940,
paul: 1942,
george: 1943,
ringo: 1940
}).saveJSON('beatles');
}
}
-
-
Updates the value associated with the given key in case it already exists
in the Dictionary. Otherwise a new key-value pair is added.
Parameters:
Name |
Type |
Description |
key |
Number
|
String
|
|
value |
Number
|
String
|
|
Example
function setup() {
let myDictionary = createStringDict('p5', 'js');
myDictionary.set('p5', 'JS');
myDictionary.print(); // logs "key: p5 - value: JS" to console
}
-
-
Returns the number of key-value pairs currently stored in the Dictionary.
Returns:
Integer
- the number of key-value pairs in the Dictionary
Example
function setup() {
let myDictionary = createNumberDict(1, 10);
myDictionary.create(2, 20);
myDictionary.create(3, 30);
print(myDictionary.size()); // logs 3 to the console
}