p5.js中p5.Table的removeTokens()方法用於從表中的值中刪除所有指定的字符。可以指定特定的列以僅從該列中刪除令牌。但是,如果未指定任何列,則將處理表中所有列和行的值。
用法:
removeTokens( chars, [column] )
參數:此函數接受上述和以下描述的兩個參數:
- chars:它是一個字符串,它指定所有必須刪除的字符。
- column:它是一個字符串或整數,用於指定要修剪的列名稱或列的ID。它是一個可選參數。
下麵的示例說明了p5.js中的removeTokens()方法。
範例1:
function setup() {
createCanvas(500, 300);
textSize(16);
tokensInput = createInput();
tokensInput.position(30, 40)
trimBtn =
createButton("Remove specified tokens");
trimBtn.position(30, 80);
trimBtn.mouseClicked(cleanTableData);
// Create the table
table = new p5.Table();
// Add two columns
table.addColumn("subject");
table.addColumn("performance");
// Add some rows to the table
let newRow = table.addRow();
newRow.setString("subject",
"----Maths---");
newRow.setString("performance",
"====Good===");
newRow = table.addRow();
newRow.setString("subject",
" English");
newRow.setString("performance",
"__-Excellent--");
newRow = table.addRow();
newRow.setString("subject",
"Science");
newRow.setString("performance",
",,, ;;OK;");
showTable();
}
function cleanTableData() {
let tokensToRemove = tokensInput.value();
// Remove given tokens only from the
// whole table
table.removeTokens(tokensToRemove);
// Redraw the table
showTable();
}
function showTable() {
clear();
// Display the rows present in the table
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 the tokens that have to be" +
" removed from the table values",
20, 20);
}
輸出:
範例2:
function setup() {
createCanvas(500, 300);
textSize(16);
tokensInput = createInput();
tokensInput.position(30, 40)
trimBtn =
createButton("Remove specified tokens");
trimBtn.position(30, 80);
trimBtn.mouseClicked(cleanTableData);
// Create the table
table = new p5.Table();
// Add two columns
table.addColumn("subject");
table.addColumn("performance");
// Add some rows to the table
let newRow = table.addRow();
newRow.setString("subject",
"----Maths---");
newRow.setString("performance",
"-----Good===");
newRow = table.addRow();
newRow.setString("subject",
"-----English---");
newRow.setString("performance",
"__-Excellent--");
newRow = table.addRow();
newRow.setString("subject",
"-Science---");
newRow.setString("performance",
",,, ;OK;");
showTable();
}
function cleanTableData() {
let tokensToRemove = tokensInput.value();
// Remove given tokens only from the
// 'name' column
table.removeTokens(tokensToRemove,
'subject');
// Redraw the table
showTable();
}
function showTable() {
clear();
// Display the rows present in the table
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 the tokens that have to be" +
" removed from the table values",
20, 20);
}
輸出:
在線編輯: https://editor.p5js.org/
環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
參考: https://p5js.org/reference/#/p5.Table/removeTokens
相關用法
- JQuery contains()用法及代碼示例
- Lodash _.xor()用法及代碼示例
- JQuery odd()用法及代碼示例
- Lodash _.nth()用法及代碼示例
- Lodash _.take()用法及代碼示例
- JQuery even()用法及代碼示例
- JQuery map()用法及代碼示例
- JQuery now()用法及代碼示例
- JQuery get()用法及代碼示例
- JQuery css()用法及代碼示例
- JQuery die()用法及代碼示例
- JQuery off()用法及代碼示例
- JQuery before()用法及代碼示例
- HTML DOM contains()用法及代碼示例
- p5.js Table set()用法及代碼示例
- JQuery is()用法及代碼示例
- JQuery add()用法及代碼示例
- HTML DOM before()用法及代碼示例
- Collect.js all()用法及代碼示例
- HTML DOM after()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.Table removeTokens() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。