The clear() method of p5.TypedDict in p5.js is used to remove all the key-value pairs in the typed dictionary. A key-value pair is a set of two values that are mapped to each other. These values can be accessed by querying this dictionary using the key portion of the pair. A typed dictionary can store multiple key-value pairs that can be accessed using the methods of the dictionary.
Syntax:
clear()
Parameters: This method does not accept any parameters.
The example below illustrates the clear() method in p5.js:
Example:
function setup() {
createCanvas(550, 500);
textSize(16);
let tmpObj = {
"Statue of Unity": "182 m",
"Spring Temple Buddha": "128 m",
"Ushiku Daibutsu": "100 m",
"Great Buddha of Thailand": "92m"
};
// Create a new dictionary
let stringDict =
createStringDict(tmpObj);
text("New string dictionary created " +
"with four keys", 20, 40);
// Check for a key
let existOne =
stringDict.hasKey('Spring Temple Buddha');
text("Dictionary has key " +
"'Spring Temple Buddha': " +
existOne, 20, 80);
// Check the current size
let currSize = stringDict.size();
text("Current size of the dictionary: " +
currSize, 20, 100);
// Clear the dictionary
stringDict.clear();
text("Dictionary has been cleared!",
20, 140);
// Check for a key again
existOne =
stringDict.hasKey('Spring Temple Buddha');
text("Dictionary has key " +
"'Spring Temple Buddha': " +
existOne, 20, 180);
// Check the current size again
currSize = stringDict.size();
text("Current size of the dictionary: " +
currSize, 20, 200);
}
Output:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/javascript/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5.TypedDict/clear