當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js Table removeTokens()用法及代碼示例

p5.j​​s中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); 
}

輸出:
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




相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.Table removeTokens() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。