p5.js NumberDict minValue() 方法
p5.js 中 p5.NumberDict 的minValue()方法用于查找数字字典中的最小值。一个数字字典可以存储多个键值对。
句法:
minValue()
参数:此方法不接受任何参数。
返回值:它返回一个数字值,它是字典中的最小值。
下面的示例说明了 p5.js 中的minValue() 方法:
例子:
Javascript
function setup() {
createCanvas(550, 300);
textSize(16);
text("Click on the button to create a " +
"new dictionary and get the lowest value",
20, 20);
setBtn =
createButton("Create random dictionary");
setBtn.position(30, 40);
setBtn.mouseClicked(createNewDict);
getBtn = createButton("Get Lowest Value");
getBtn.position(300, 40);
getBtn.mouseClicked(getLowestValue);
}
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 > 50) ? 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 lowest value",
20, 20);
}
function getLowestValue() {
// Get the lowest value in the dictionary
let lowestVal = numDict.minValue();
// Display the lowest value
text("The lowest value in the dictionary is: " +
lowestVal, 20, 240);
text("Click on the button to create a " +
"new dictionary and get the lowest value",
20, 20);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.NumberDict/minValue