p5.js 表 getRows() 方法
p5.js 中 p5.Table 的getRows() 方法用于以 p5.TableRow 对象数组的形式返回对表中所有行的引用。每个返回的行对象都可用于根据需要获取和设置值。
句法:
getRows()
参数:此函数不接受任何参数。
下面的示例说明了 p5.js 中的getRows()函数:
例子:
javascript
function setup() {
createCanvas(500, 400);
textSize(16);
text("Click on the button to display" +
"all the rows in the table", 20, 20);
getColBtn = createButton("Show all rows");
getColBtn.position(30, 50);
getColBtn.mouseClicked(showAllRows);
// Create the table
table = new p5.Table();
// Add two columns
table.addColumn("movie");
table.addColumn("rating");
table.addColumn("price");
// Add 10 randomly generated rows
for (let i = 0; i < 10; i++) {
let newRow = table.addRow();
newRow.setString("movie",
"Movie " + floor(random(1, 100)));
newRow.setString("rating",
floor(random(1, 5)));
newRow.setString("price",
"$" + floor(random(10, 100)));
}
}
function showAllRows() {
clear();
let currentRows = table.getRows();
// Display the total rows
// present in the table
text("There are " +
currentRows.length +
" rows in the table", 20, 100);
for (let r = 0; r < currentRows.length; r++)
text(currentRows[r].arr.toString(),
20, 140 + r * 20);
text("Click on the button to display" +
"all the rows in the table", 20, 20);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.Table/getRows