📜  p5.js TypedDict clear() 方法

📅  最后修改于: 2022-05-13 01:56:35.857000             🧑  作者: Mango

p5.js TypedDict clear() 方法

p5.js 中 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