p5.js中p5.Table的matchRows()方法用於查找與給定正則表達式匹配的所有行。它以數組形式返回對匹配行的引用。該方法用於搜索行的列可以指定為參數。
用法:
matchRows( regexp, [column] )
參數:該方法接受上述和以下所述的兩個參數:
- regexp:它是一個String或RegExp對象,它指定要匹配的正則表達式。
- column:它是一個字符串或數字,表示列的名稱或列的ID。它是一個可選參數。
返回值:此方法返回匹配給定正則表達式的p5.TableRow對象的數組。
以下示例說明了p5.js中的match Rows()方法:
例:
function setup() {
createCanvas(500, 300);
textSize(16);
matchQueryInput =
createInput();
matchQueryInput.position(30, 40);
getColBtn =
createButton("Get the matching row");
getColBtn.position(30, 70);
getColBtn.mouseClicked(getMatchedResults);
// Create the table
table = new p5.Table();
// Add two columns
table.addColumn("name");
table.addColumn("id");
// Add some rows to the table
let newRow = table.addRow();
newRow.setString("name", "mary");
newRow.setString("id", 21);
newRow = table.addRow();
newRow.setString("name", "marco6");
newRow.setString("id", 27);
newRow = table.addRow();
newRow.setString("name", "tunisia 4");
newRow.setString("id", 32);
newRow = table.addRow();
newRow.setString("name", "user 23");
newRow.setString("id", 32);
newRow = table.addRow();
newRow.setString("name", "admin");
newRow.setString("id", 45);
newRow = table.addRow();
newRow.setString("name", "mikasa");
newRow.setString("id", 23);
showTable();
}
function getMatchedResults() {
clear();
let matchQuery =
matchQueryInput.value();
if (matchQuery != "") {
// Match the query in the column of 'name'
matchResults =
table.matchRows(new RegExp(matchQuery),
'name');
if (matchResults.length > 0) {
text("The rows that matches the " +
"query is", 20, 120);
for (let i = 0; i < matchResults.length; i++) {
// Display the matched value
text(matchResults[i].arr[0], 20, 140 + i * 20);
text(matchResults[i].arr[1], 120, 140 + i * 20);
}
}
else text("No Results Found", 20, 120);
} else {
text("The query string is empty", 20, 120);
}
text("Enter a string to match it in " +
"the 'name' column table", 20, 20);
}
function showTable() {
clear();
// Display the total rows present in the table
text("There are " + table.getRowCount() +
" rows in the table", 20, 120);
for (let r = 0; r < table.getRowCount(); r++)
for (let c = 0; c < table.getColumnCount(); c++)
text(table.getString(r, c),
20 + c * 100, 140 + r * 20);
text("Enter a string to match it in " +
"the 'name' column table", 20, 20);
}
輸出:
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考:https://p5js.org/reference/#/p5.Table/matchRows
相關用法
- Collect.js all()用法及代碼示例
- JQuery map()用法及代碼示例
- p5.js Table get()用法及代碼示例
- JQuery is()用法及代碼示例
- JQuery sub()用法及代碼示例
- Lodash _.nth()用法及代碼示例
- JQuery get()用法及代碼示例
- JQuery css()用法及代碼示例
- JQuery contains()用法及代碼示例
- JQuery off()用法及代碼示例
- JQuery before()用法及代碼示例
- Collect.js avg()用法及代碼示例
- Lodash _.xor()用法及代碼示例
- JQuery die()用法及代碼示例
- p5.js Table set()用法及代碼示例
- Lodash _.take()用法及代碼示例
- HTML DOM contains()用法及代碼示例
- JQuery even()用法及代碼示例
- JQuery odd()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.Table matchRows() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。