當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


p5.js NumberDict minKey()用法及代碼示例


p5.j​​s中p5.NumberDict的minKey()方法用於查找數字字典中鍵的最小值。鍵值對是一組相互映射的兩個值。可以通過使用該對的關鍵字部分查詢此字典來訪問這些值。數字字典可以存儲可以使用字典方法訪問的多個鍵-值對。

用法:

minKey()

參數:該函數不接受任何參數。

返回值:它返回一個Number值,該值指定數字字典中的最低鍵。

以下示例說明了p5.js中的minKey()方法:



範例1:

Javascript

function setup() { 
  createCanvas(500, 300); 
  textSize(16); 
  
  text("Click on the button to add new " + 
       "values or get the lowest key", 
       20, 20); 
  
  text("Key:", 20, 60); 
  text("Value:", 160, 60); 
  
  key_input = createInput('1'); 
  key_input.position(70, 50); 
  key_input.size(40); 
  
  val_input = createInput('1'); 
  val_input.position(220, 50); 
  val_input.size(40); 
  
  setBtn = createButton("Add new item"); 
  setBtn.position(30, 100); 
  setBtn.mouseClicked(createNewDict); 
  
  getBtn = createButton("Get Lowest Key"); 
  getBtn.position(160, 100); 
  getBtn.mouseClicked(getLowestKey); 
  
  // Create a Number Dictionary initially 
  numDict = createNumberDict(100, 0); 
} 
  
function createNewDict() { 
  clear(); 
  
  let key = int(key_input.value()); 
  let val = int(val_input.value()); 
  
  numDict.set(key, val); 
  
  text("New key-value added to dictionary", 
       20, 160); 
  
  text("Key:", 20, 60); 
  text("Value:", 160, 60); 
  text("Click on the button to add new " + 
       "values or get the lowest key", 
       20, 20); 
} 
  
function getLowestKey() { 
  
  // Get the lowest key in the dictionary 
  let lowestKey = numDict.minKey(); 
  
  // Display the lowest key 
  text("The lowest key in the dictionary is:"
       + lowestKey, 20, 200); 
  
  text("Key:", 20, 60); 
  text("Value:", 160, 60); 
  text("Click on the button to add new " + 
       "values or get the lowest key", 
       20, 20); 
}

輸出:

範例2:

Javascript

function setup() { 
  createCanvas(550, 300); 
  textSize(16); 
  
  text("Click on the button to create a " + 
       "new dictionary and get the lowest key", 
       20, 20); 
  
  setBtn = 
    createButton("Create random dictionary"); 
  setBtn.position(30, 40); 
  setBtn.mouseClicked(createNewDict); 
  
  getBtn = createButton("Get Lowest Key"); 
  getBtn.position(300, 40); 
  getBtn.mouseClicked(getLowestKey); 
} 
  
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); 
    obj[rk] = rn; 
  
    text("Key:" + rk + ":Value:" + 
         rn, 40, 120 + 20 * i); 
  } 
  
  // Create a number dict using the 
  // above values 
  numDict = createNumberDict(obj); 
  
  text("New Dictionary created with values", 
       20, 80); 
  
  text("Click on the button to create a " + 
       "new dictionary and get the lowest key", 
       20, 20); 
} 
  
function getLowestKey() { 
  
  // Get the lowest key in the dictionary 
  let lowestKey = numDict.minKey(); 
  
  // Display the lowest key 
  text("The lowest key in the dictionary is:" + 
       lowestKey, 20, 240); 
  
  text("Click on the button to create a " + 
       "new dictionary and get the lowest key", 
       20, 20); 
}

輸出:

在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5.NumberDict/minKey

相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js NumberDict minKey() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。