p5.js中p5.NumberDict的maxValue()方法用於查找數字字典中的最大值。數字字典可以存儲多個鍵值對。
用法:
maxValue()
參數:此方法不接受任何參數。
返回值:它返回一個Number值,它是字典中的最高值。
以下示例說明了p5.js中的maxValue()方法:
例:
Javascript
function setup() { 
  createCanvas(550, 300); 
  textSize(16); 
  
  text("Click on the button to create a new " + 
       "dictionary and get the highest value", 
       20, 20); 
  
  setBtn =  
    createButton("Create random dictionary"); 
  setBtn.position(30, 40); 
  setBtn.mouseClicked(createNewDict); 
  
  getBtn =  
    createButton("Get highest Value"); 
  getBtn.position(300, 40); 
  getBtn.mouseClicked(getHighestValue); 
} 
  
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); 
    rn = (rk > 25) ? rn:-rn; 
  
    obj[rk] = rn; 
  
    text("Key:" + rk + ":Value:" + 
         rn, 40, 120 + 20 * i); 
  } 
  
  // Create a dictionary 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 highest value", 
       20, 20); 
} 
  
function getHighestValue() { 
  
  // Get the highest value in the dictionary 
  let highestVal = numDict.maxValue(); 
  
  // Display the highest value 
  text("The highest value in the dictionary is:" + 
       highestVal, 20, 240); 
  
  text("Click on the button to create a new " + 
       "dictionary and get the highest value", 
       20, 20); 
}輸出:

在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考:https://p5js.org/reference/#/p5.NumberDict/maxValue
相關用法
- p5.js NumberDict minValue()用法及代碼示例
 - p5.js NumberDict minKey()用法及代碼示例
 - p5.js NumberDict maxKey()用法及代碼示例
 - p5.js NumberDict div()用法及代碼示例
 - p5.js NumberDict mult()用法及代碼示例
 - p5.js NumberDict sub()用法及代碼示例
 - p5.js NumberDict add()用法及代碼示例
 - Lodash _.method()用法及代碼示例
 - Node.js Http2ServerRequest.method用法及代碼示例
 - Node.js http.IncomingMessage.method用法及代碼示例
 - Javascript dataView.getInt16()用法及代碼示例
 - Javascript RegExp toString()用法及代碼示例
 - Node.js URLSearchParams.has()用法及代碼示例
 - JavaScript Math cosh()用法及代碼示例
 - HTML DOM isEqualNode()用法及代碼示例
 - JavaScript Date toLocaleTimeString()用法及代碼示例
 - Node.js crypto.createHash()用法及代碼示例
 - Node.js writeStream.clearLine()用法及代碼示例
 - Node.js fs.link()用法及代碼示例
 - JavaScript Math random()用法及代碼示例
 - JavaScript Math round()用法及代碼示例
 - Javascript toString()用法及代碼示例
 
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js NumberDict maxValue() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
