当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


p5.js Table matchRow()用法及代码示例

p5.j​​s中p5.Table的matchRow()方法用于查找与给定正则表达式匹配的第一行,并返回对该行的引用。该方法用于搜索行的列可以指定为参数。即使存在正则表达式的多个匹配项,该方法也仅返回可能匹配项的第一行。

用法:

matchRow( regexp, column )

参数:该方法接受上述和以下所述的两个参数:

  • regexp:它是一个String或RegExp对象,它指定要匹配的正则表达式。
  • column:它是一个字符串或数字,表示列的名称或列的ID。

返回值:此方法返回一个p5.TableRow对象,该对象与给定的正则表达式匹配。

以下示例说明了p5.js中的match Row()方法:



例:

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(getMatchResults); 
  
  // 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 getMatchResults() { 
  clear(); 
  
  let matchQuery = 
      matchQueryInput.value(); 
  
  // Get the row values using matchRow() 
  if (matchQuery != "") { 
    
    // Match the query in the column of 'name' 
    matchResults = 
      table.matchRow(new RegExp(matchQuery), 
                     "name"); 
  
    if (matchResults) { 
      text("The row that matches the " +  
           "query is", 20, 120); 
  
      // Display the matched value 
      text(matchResults.arr[0], 20, 140); 
      text(matchResults.arr[1], 120, 140); 
    } else text("No Results Found", 20, 120); 
  } else { 
    text("The query string is empty", 20, 120); 
  } 
  text("Enter a string to find 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 find 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/matchRow




相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 p5.Table matchRow() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。