Wednesday, July 3, 2024
HomeLanguagesJavascriptp5.js TypedDict remove() Method

p5.js TypedDict remove() Method

The remove() method of p5.TypedDict in p5.js is used to remove the given key-value pair from 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: 

remove( key )

Parameters: This method accepts a single parameter as shown above and discussed below:

  • key: This is a string that denotes the key that has to be checked in the dictionary.

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

Example:

Javascript




function setup() {
  createCanvas(550, 400);
  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 string dictionary
  let stringDict =
      createStringDict(tmpObj);
  text("New string dictionary created", 20, 20);
  
  // Checking the size of the dictionary
  let currSize = stringDict.size();
  text("The current size of the " +
       "dictionary is: " + currSize, 20, 40);
  
  let existOne =
      stringDict.hasKey("Statue of Unity");
  text("Dictionary has key " +
       "'Statue of Unity': " +
       existOne, 20, 80);
  
  let existTwo =
      stringDict.hasKey("Spring Temple Buddha");
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 100);
  
  // Removing one key
  stringDict.remove("Spring Temple Buddha");
  text("One key removed from the dictionary",
       20, 140);
  
  // Checking the size of the dictionary again
  currSize = stringDict.size();
  text("The current size of the dictionary is: " +
       currSize, 20, 160);
  
  // Checking for the removed key
  existTwo =
    stringDict.hasKey("Spring Temple Buddha");
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 200);
  
  // Checking the other keys
  let existThree = 
      stringDict.hasKey("Ushiku Daibutsu");
  text("Dictionary has key " +
       "'Ushiku Daibutsu': " +
       existThree, 20, 220);
}


Output:

Online editor: https://editor.p5js.org/
Environment Setup: https://www.neveropen.co.za/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5.TypedDict/remove

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!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments