Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

p5.js TypedDict clear() Method

  • Last Updated : 25 Nov, 2020

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: 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

clear()

Parameters: This method does not accept any parameters.

The example below illustrates the clear() method in p5.js:



Example:

Javascript




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/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5.TypedDict/clear




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!