当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


p5.js NumberDict maxKey()用法及代码示例


p5.j​​s中p5.NumberDict的maxKey()方法用于查找数字字典中的最高键值。键值对是一组相互映射的两个值。可以通过使用该对的关键字部分查询此字典来访问这些值。数字字典可以存储可以使用字典方法访问的多个键-值对。

用法:

maxKey()

参数:该函数不接受任何参数。

返回值:它返回一个Number值,它是数字字典中的最高键。

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



范例1:

Javascript

function setup() { 
  createCanvas(500, 300); 
  textSize(16); 
  
  text("Click on the button to add new " + 
       "values or get the highest 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(addtoDict); 
  
  getBtn = createButton("Get Highest Key"); 
  getBtn.position(160, 100); 
  getBtn.mouseClicked(getHighestKey); 
  
  // Create a Number Dictionary initially 
  numDict = createNumberDict(100, 0); 
} 
  
function addtoDict() { 
  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 highest key", 
       20, 20); 
} 
  
function getHighestKey() { 
  
  // Get the highest key in the dictionary 
  let highestKey = numDict.maxKey(); 
  
  // Display the highest key 
  text("The highest key in the dictionary is:" + 
       highestKey, 20, 200); 
  
  text("Key:", 20, 60); 
  text("Value:", 160, 60); 
  text("Click on the button to add new " + 
       "values or get the highest 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 highest key", 
       20, 20); 
  
  setBtn =  
    createButton("Create random dictionary"); 
  setBtn.position(30, 40); 
  setBtn.mouseClicked(createNewDict); 
  
  getBtn = createButton("Get Highest Key"); 
  getBtn.position(300, 40); 
  getBtn.mouseClicked(getHighestKey); 
} 
  
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 highest key", 
       20, 20); 
} 
  
function getHighestKey() { 
  
  // Get the highest key in the dictionary 
  let highestKey = numDict.maxKey(); 
  
  // Display the lowest key 
  text("The highest key in the dictionary is:" + 
       highestKey, 20, 240); 
  
  text("Click on the button to create a " + 
       "new dictionary and get the highest key", 
       20, 20); 
}

输出:

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

相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 p5.js NumberDict maxKey() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。