📜  p5.js TypedDict hasKey() 方法

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

p5.js TypedDict hasKey() 方法

p5.js 中 p5.TypedDict 的hasKey() 方法用于检查给定键是否存在于类型化字典中。如果给定键存在,则此方法返回true ,否则返回false 。键值对是一组相互映射的两个值。可以通过使用该对的键部分查询此字典来访问这些值。类型化字典可以存储多个键值对,可以使用字典的方法访问这些键值对。

句法:

hasKey( key )

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

  • key:这是一个数字,表示必须在字典中检查的键。

以下示例说明了 p5.js 中的hasKey() 方法

示例 1:

Javascript
let y = 0;
  
function setup() {
  createCanvas(550, 500);
  textSize(16);
  
  text("Click the button to create a new " +
       "dictionary or check if the keys exist",
       20, 20);
  
  text("Key:", 20, 260);
  
  key_input = createInput('k0');
  key_input.position(70, 250);
  key_input.size(40);
  
  createBtn = createButton("Create dictionary");
  createBtn.position(30, 40);
  createBtn.mouseClicked(createNewDict);
  
  checkBtn = createButton("Check if the key exists");
  checkBtn.position(30, 290);
  checkBtn.mouseClicked(checkVal);
}
  
function createNewDict() {
  clear();
  
  // Create an object with random values
  let obj = {};
  for (let i = 0; i < 5; i++) {
    let rk = "k" + i;
    let rn = "v" + i;
    obj[rk] = rn;
  
    text("Key: " + rk + "  Value: " +
         rn, 40, 120 + 20 * i);
  }
  
  // Create a string dict using the above values
  numDict = createStringDict(obj);
  
  text("New Dictionary created with values",
       20, 80);
  
  text("Click the button to create a new " +
       "dictionary or check if the keys exist",
       20, 20);
  
  text("Key:", 20, 260);
}
  
function checkVal() {
  
  // Get the key to be checked
  let keyToCheck = key_input.value();
  
  // Use hasKey() to check if the key exists
  let hasEntry = numDict.hasKey(keyToCheck);
  
  // If the key exists in the dictionary
  if (hasEntry) {
  
    let keyVal = numDict.get(keyToCheck);
    
    text("The value at key: " + keyToCheck +
         " is: " + keyVal, 20, 340 + y * 20);
  }
  
  // The key does not exist
  else {
    text("The key does not exist",
         20, 340 + y * 20);
  }
  y++;
  
  text("Click the button to create a new " +
       "dictionary or check if the keys exist",
       20, 20);
  
  text("Key:", 20, 260);
}


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);
  
  // Check if the specified key exists
  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 createKey()", 20, 140)
  
  // Check if the specified key exists again
  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);
  
  // Check if the specified key exists
  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 createKey()", 20, 140)
  
  // Check if the specified key exists again
  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/hasKey