p5.js TypedDict print() 方法
p5.js 中 p5.TypedDict 的print() 方法用于将字典中当前存在的所有键值对打印到控制台。键值对是一组相互映射的两个值。可以通过使用该对的键部分查询此字典来访问这些值。类型化字典可以存储多个键值对,可以使用字典的方法访问这些键值对。
句法:
print()
参数:此方法不接受任何参数。
下面的示例说明了 p5.js 中的print() 方法:
例子:
Javascript
function setup() {
createCanvas(550, 300);
textSize(16);
let stringDict =
createStringDict("Tokyo", "37.26 million");
text("New string dictionary created " +
"with one key", 20, 20);
// Getting the current size of the dictionary
let currSize = stringDict.size();
print("Printing " + currSize +
" key-value pair(s):");
// Printing all the values to console
stringDict.print();
text("Dictionary keys printed to console",
20, 60);
let tmpObj = {
"Delhi": "25.87 million",
"Shanghai": "23.48 million",
"Mexico City": "21.34 million"
};
// Add the given key to the dictionary
// specifying the key and value as an object
stringDict.create(tmpObj);
text("New keys added to the dictionary",
20, 100);
// Getting the current size of the dictionary
currSize = stringDict.size();
print("Printing " + currSize +
" key-value pair(s):");
// Printing all the values to console
stringDict.print();
text("Dictionary keys printed to console",
20, 140);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.TypedDict/print