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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。