📜  p5.js TypedDict set() 方法

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

p5.js TypedDict set() 方法

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

句法:

set( key, value )

参数:此方法接受两个参数,如上所示并在下面讨论:

  • key:这是一个数字或字符串,表示必须添加到字典中的键。
  • value:这是一个数字或字符串,表示必须添加到字典中的值。

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

例子:

Javascript
function setup() {
  createCanvas(550, 300);
  textSize(16);
  
  // Create an empty dictionary
  let stringDict = 
      createStringDict({});
  text("New empty string dictionary created",
       20, 20);
  
  // Get the value at the given key
  let valOne = stringDict.get('Apple');
  text("The value at key 'Apple': " +
       valOne, 20, 60);
  
  // Add the given key to the dictionary
  // specifying the key and value
  stringDict.set('Apple', '$340 billion');
  
  text("New key 'Apple' added with " +
       "set() method", 20, 100)
  
  // Get the value at the given key
  valOne = stringDict.get('Apple');
  text("The value at key 'Apple': " +
       valOne, 20, 140);
  
  // Change the value of an already set key
  stringDict.set('Apple', '$352 billion');
  
  text("Value at key 'Apple' modified using " +
       "the set() method", 20, 180)
  
  // Get the value at the given key
  valOne = stringDict.get('Apple');
  text("The value at key 'Apple': " +
       valOne, 20, 220);
}


输出:

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