Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptp5.js TypedDict clear() Method

p5.js TypedDict clear() Method

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:

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments