p5.js TypedDict remove() 方法
p5.js 中 p5.TypedDict 的remove() 方法用于从类型化字典中删除给定的键值对。键值对是一组相互映射的两个值。可以通过使用该对的键部分查询此字典来访问这些值。类型化字典可以存储多个键值对,可以使用字典的方法访问这些键值对。
句法:
remove( key )
参数:此方法接受一个参数,如上所示并在下面讨论:
- key:这是一个字符串,表示必须在字典中检查的键。
下面的示例说明了 p5.js 中的remove() 方法:
例子:
Javascript
function setup() {
createCanvas(550, 400);
textSize(16);
let tmpObj = {
"Statue of Unity": "182 m",
"Spring Temple Buddha": "128 m",
"Ushiku Daibutsu": "100 m",
"Great Buddha of Thailand": "92m"
};
// Create a new string dictionary
let stringDict =
createStringDict(tmpObj);
text("New string dictionary created", 20, 20);
// Checking the size of the dictionary
let currSize = stringDict.size();
text("The current size of the " +
"dictionary is: " + currSize, 20, 40);
let existOne =
stringDict.hasKey("Statue of Unity");
text("Dictionary has key " +
"'Statue of Unity': " +
existOne, 20, 80);
let existTwo =
stringDict.hasKey("Spring Temple Buddha");
text("Dictionary has key " +
"'Spring Temple Buddha': " +
existTwo, 20, 100);
// Removing one key
stringDict.remove("Spring Temple Buddha");
text("One key removed from the dictionary",
20, 140);
// Checking the size of the dictionary again
currSize = stringDict.size();
text("The current size of the dictionary is: " +
currSize, 20, 160);
// Checking for the removed key
existTwo =
stringDict.hasKey("Spring Temple Buddha");
text("Dictionary has key " +
"'Spring Temple Buddha': " +
existTwo, 20, 200);
// Checking the other keys
let existThree =
stringDict.hasKey("Ushiku Daibutsu");
text("Dictionary has key " +
"'Ushiku Daibutsu': " +
existThree, 20, 220);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.TypedDict/remove