p5.js中p5.TypedDict的hasKey()方法用于检查键入的字典中是否存在给定的键。如果给定键存在,则此方法返回true,否则返回false。键值对是一组相互映射的两个值。可以通过使用该对的关键字部分查询此字典来访问这些值。一个类型化的字典可以存储可以使用字典方法访问的多个键-值对。
用法:
hasKey( key )
参数:此方法接受单个参数,如上所示和以下讨论:
- key:这是一个数字,表示必须在字典中检查的 key 。
以下示例说明了p5.js中的hasKey()方法:
范例1:
Javascript
let y = 0;
function setup() {
createCanvas(550, 500);
textSize(16);
text("Click the button to create a new " +
"dictionary or check if the keys exist",
20, 20);
text("Key:", 20, 260);
key_input = createInput('k0');
key_input.position(70, 250);
key_input.size(40);
createBtn = createButton("Create dictionary");
createBtn.position(30, 40);
createBtn.mouseClicked(createNewDict);
checkBtn = createButton("Check if the key exists");
checkBtn.position(30, 290);
checkBtn.mouseClicked(checkVal);
}
function createNewDict() {
clear();
// Create an object with random values
let obj = {};
for (let i = 0; i < 5; i++) {
let rk = "k" + i;
let rn = "v" + i;
obj[rk] = rn;
text("Key:" + rk + " Value:" +
rn, 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 button to create a new " +
"dictionary or check if the keys exist",
20, 20);
text("Key:", 20, 260);
}
function checkVal() {
// Get the key to be checked
let keyToCheck = key_input.value();
// Use hasKey() to check if the key exists
let hasEntry = numDict.hasKey(keyToCheck);
// If the key exists in the dictionary
if (hasEntry) {
let keyVal = numDict.get(keyToCheck);
text("The value at key:" + keyToCheck +
" is:" + keyVal, 20, 340 + y * 20);
}
// The key does not exist
else {
text("The key does not exist",
20, 340 + y * 20);
}
y++;
text("Click the button to create a new " +
"dictionary or check if the keys exist",
20, 20);
text("Key:", 20, 260);
}
输出:
范例2:
Javascript
function setup() {
createCanvas(550, 300);
textSize(16);
let stringDict =
createStringDict('Statue of Unity',
'182 m');
text("New string dictionary created " +
"with one key", 20, 20);
// Check if the specified key exists
let existOne =
stringDict.hasKey('Statue of Unity');
text("Dictionary has key 'Statue of Unity':" +
existOne, 20, 60);
let existTwo =
stringDict.hasKey('Spring Temple Buddha');
text("Dictionary has key " +
"'Spring Temple Buddha':" +
existTwo, 20, 100);
// Add the given key to the dictionary
// specifying the key and value
stringDict.create('Spring Temple Buddha',
'128 m');
text("New key 'Spring Temple Buddha' " +
"added with createKey()", 20, 140)
// Check if the specified key exists again
existTwo =
stringDict.hasKey('Spring Temple Buddha');
text("Dictionary has key " +
"'Spring Temple Buddha':" +
existTwo, 20, 180);
}
输出:
在线编辑: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.TypedDict/hasKey
相关用法
- p5.js TypedDict set()用法及代码示例
- p5.js TypedDict get()用法及代码示例
- p5.js TypedDict create()用法及代码示例
- p5.js TypedDict clear()用法及代码示例
- p5.js TypedDict size()用法及代码示例
- p5.js TypedDict print()用法及代码示例
- p5.js TypedDict remove()用法及代码示例
- 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 TypedDict hasKey() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。