📜  p5.Table removeTokens() 方法

📅  最后修改于: 2022-05-13 01:56:24.116000             🧑  作者: Mango

p5.Table removeTokens() 方法

p5.js 中 p5.Table 的removeTokens() 方法用于从表中的值中删除所有指定的字符。可以指定特定列以仅从该列中删除标记。但是,如果未指定列,则处理表中所有列和行的值。

句法:

removeTokens( chars, [column] )

参数:该函数接受上面提到的两个参数,如下所述:

  • chars:它是一个字符串,指定所有必须删除的字符。
  • column:它是一个String或整数,指定要修剪的列的列名或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);
}

输出:
removeToken-ex1

示例 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);
}

输出:
removeToken-ex2

在线编辑器: https://editor.p5js.org/

环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

参考: https://p5js.org/reference/#/p5.Table/removeTokens