📜  p5.js TypedDict create() 方法

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

p5.js TypedDict create() 方法

p5.js 中 p5.TypedDict 的create() 方法用于将给定的键值对或对的集合添加到字典中。键值对是一组相互映射的两个值。可以通过使用该对的键部分查询此字典来访问这些值。字典可以存储多个键值对,可以使用字典的方法访问这些键值对。

句法:

create( key, value )

要么

create( obj )

参数:

  • key :它指定用作要添加到字典的键的字符串。
  • value :它指定用作要添加到字典中的值的字符串。
  • obj :它指定包含要添加到字典中的键值对的对象。

下面的示例说明了 p5.js 中的create() 方法

示例 1:

Javascript
function setup() {
  createCanvas(550, 300);
  textSize(16);
  
  let stringDict =
      createStringDict("Statue of Unity", "182 m");
  text("New string dictionary created " +
       "with one key", 20, 20);
  
  let existOne = 
      stringDict.hasKey("Statue of Unity");
  text("Dictionary has key " +
       "'Statue of Unity': " +
       existOne, 20, 60);
  
  let existTwo = 
      stringDict.hasKey("Spring Temple Buddha");
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 100);
  
  let tmpObj = {
    "Spring Temple Buddha": "128 m",
    "Ushiku Daibutsu": "100 m",
    "Great Buddha of Thailand": "92m"
  };
  
  // Add the given key to the dictionary
  // specifying the key and value as an object
  stringDict.create(tmpObj);
  text("New keys added with create()",
       20, 140);
  
  existTwo = 
    stringDict.hasKey("Spring Temple Buddha");
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 180);
  
  let existThree =
      stringDict.hasKey("Ushiku Daibutsu");
  text("Dictionary has key " +
       "'Ushiku Daibutsu': " + 
       existThree, 20, 220);
}


Javascript
function setup() {
  createCanvas(550, 300);
  textSize(16);
  
  let stringDict =
      createStringDict('Statue of Unity',
                       '182 m');
  text("New string dictionary " +
       "created with one key", 20, 20);
  
  let existOne =
      stringDict.hasKey('Statue of Unity');
  text("Dictionary has key 'Statue of Unity': "
        + existOne, 20, 60);
  
  let existTwo =
      stringDict.hasKey('Spring Temple Buddha');
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 100);
  
  // Add the given key to the dictionary
  // specifying the key and value
  stringDict.create('Spring Temple Buddha',
                    '128 m');
  text("New key 'Spring Temple Buddha'" +
       " added with create()", 20, 140)
  
  existTwo = 
    stringDict.hasKey('Spring Temple Buddha');
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 180);
}


输出:

示例 2:

Javascript

function setup() {
  createCanvas(550, 300);
  textSize(16);
  
  let stringDict =
      createStringDict('Statue of Unity',
                       '182 m');
  text("New string dictionary " +
       "created with one key", 20, 20);
  
  let existOne =
      stringDict.hasKey('Statue of Unity');
  text("Dictionary has key 'Statue of Unity': "
        + existOne, 20, 60);
  
  let existTwo =
      stringDict.hasKey('Spring Temple Buddha');
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 100);
  
  // Add the given key to the dictionary
  // specifying the key and value
  stringDict.create('Spring Temple Buddha',
                    '128 m');
  text("New key 'Spring Temple Buddha'" +
       " added with create()", 20, 140)
  
  existTwo = 
    stringDict.hasKey('Spring Temple Buddha');
  text("Dictionary has key " +
       "'Spring Temple Buddha': " +
       existTwo, 20, 180);
}

输出:

在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.TypedDict/create