Related Articles

Related Articles

p5.js TypedDict size() Method
  • Last Updated : 25 Nov, 2020

The size() method of p5.TypedDict in p5.js is used to get the current size of the dictionary. The size represents the number of key-value pairs currently present in the 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: 

size()

Parameters: This method does not accept any parameters.

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

Example:



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

let tmp = 1;
  
function setup() {
  createCanvas(550, 500);
  textSize(16);
  
  text("Click the button to add a new entry " +
       "or check the size of the dictionary",
       20, 20);
  
  addBtn = createButton("Add a new entry");
  addBtn.position(30, 40);
  addBtn.mouseClicked(addEntry);
  
  checkBtn =
    createButton("Check size of dictionary");
  checkBtn.position(30, 80);
  checkBtn.mouseClicked(checkSize);
  
  // Create a string dictionary with one entry
  numDict = createStringDict('k0', 'v0');
}
  
function addEntry() {
  
  // Add a new entry to the dictionary
  numDict.create("k" + tmp, "v" + tmp);
  
  text("New Entry added to the dictionary",
       20, 120 + tmp * 20);
    
  tmp++;
}
  
function checkSize() {
  
  // Get the current size of the dictionary
  let currSize = numDict.size();
  
  // Display the current size
  text("The current size of the dictionary is: " +
       currSize, 20, 120 + tmp * 20);
  
  tmp++;
}

chevron_right


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/size

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :