p5.js中p5.TypedDict的get()方法用於返回字典給定鍵處的值。如果字典中不存在鍵,則該方法返回undefined。鍵值對是一組相互映射的兩個值。可以通過使用該對的關鍵字部分查詢此字典來訪問這些值。一個類型化的字典可以存儲可以使用字典方法訪問的多個鍵/值對。
用法:
get( key )
參數:此方法接受單個參數,如上所示和以下討論:
- key:這是一個數字或字符串,表示必須添加到字典中的鍵。
 
返回值:此方法返回一個數字或字符串,表示存儲在給定鍵處的值。
以下示例說明了p5.js中的get()方法:
例:
Javascript
let y = 0; 
  
function setup() { 
  createCanvas(550, 500); 
  textSize(16); 
  
  text("Click the buttons to create a " + 
       "dictionary or get the value of the key", 
       20, 20); 
  
  text("Key:", 20, 260); 
  
  key_input = createInput('user'); 
  key_input.position(70, 250); 
  key_input.size(80); 
  
  setBtn =  
    createButton("Create dictionary"); 
  setBtn.position(30, 40); 
  setBtn.mouseClicked(createNewDict); 
  
  getBtn =  
    createButton("Get the value of the given key"); 
  getBtn.position(30, 290); 
  getBtn.mouseClicked(getVal); 
} 
  
function createNewDict() { 
  clear(); 
  
  // Create an object with random values 
  let obj = {}; 
  for (let i = 0; i < 5; i++) { 
    let rk = ceil(Math.random() * 100); 
    let rn = floor(Math.random() * 100); 
  
    let rkey = "user" + rk; 
    let rval = "data" + rn; 
    obj[rkey] = rval; 
  
    text("Key:" + rkey + "  Value:" + 
         rval, 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 buttons to create a " + 
       "dictionary or get the value of the key", 
       20, 20); 
  
  text("Key:", 20, 260); 
} 
  
function getVal() { 
  
  // Get the key to be retrieved 
  let keyToCheck = key_input.value(); 
  
  // Get the value of the key from the dictionary 
  let keyVal = numDict.get(keyToCheck); 
  
  text("The value at key:" + keyToCheck + 
       " is:" + keyVal, 20, 340 + y * 20); 
  
  y++; 
  
  text("Click the buttons to create a " + 
       "dictionary or get the value of the key", 
       20, 20); 
}輸出:

在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5.TypedDict/get
相關用法
- p5.js TypedDict set()用法及代碼示例
 - p5.js TypedDict create()用法及代碼示例
 - p5.js TypedDict hasKey()用法及代碼示例
 - p5.js TypedDict clear()用法及代碼示例
 - p5.js TypedDict size()用法及代碼示例
 - p5.js TypedDict print()用法及代碼示例
 - p5.js TypedDict remove()用法及代碼示例
 - Javascript weakMap.get()用法及代碼示例
 - JQuery get()用法及代碼示例
 - JQuery Misc get()用法及代碼示例
 - Javascript Reflect.get()用法及代碼示例
 - Javascript handler.get()用法及代碼示例
 - HTML DOM customElements get()用法及代碼示例
 - p5.js Table get()用法及代碼示例
 - p5.js TableRow get()用法及代碼示例
 - Lodash _.get()用法及代碼示例
 - d3.js group.get()用法及代碼示例
 
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js TypedDict get() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
