當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


p5.js TypedDict clear()用法及代碼示例


p5.j​​s中p5.TypedDict的clear()方法用於刪除類型字典中的所有鍵值對。鍵值對是一組相互映射的兩個值。可以通過使用該對的關鍵字部分查詢此字典來訪問這些值。一個類型化的字典可以存儲可以使用字典方法訪問的多個鍵-值對。

用法:

clear()

參數:此方法不接受任何參數。

以下示例說明了p5.js中的clear()方法:

例:



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); 
}

輸出:

在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5.TypedDict/clear

相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js TypedDict clear() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。