📜  p5.js p5.Table findRow() 方法

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

p5.js p5.Table findRow() 方法

p5.js 中 p5.Table 的findRow() 方法用于查找包含给定和值的第一行,并向该行返回一个值。该方法用于搜索行的列可以指定为参数。即使存在多个匹配项,该方法也仅返回可能匹配项的第一行。

句法:

findRow( value, column )

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

  • value:它是一个字符串,指定必须匹配的值。
  • column:它是一个String或Number,表示要搜索的列的列名或列ID。

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

示例 1:

function setup() {
  createCanvas(500, 300);
  textSize(16);
   
  findQueryInput = createInput();
  findQueryInput.position(30, 40);
   
  getColBtn =
    createButton("Get the matching row");
  getColBtn.position(30, 70);
  getColBtn.mouseClicked(getFindResults);
   
  // Create the table
  table = new p5.Table();
   
  // Add two columns
  table.addColumn("name");
  table.addColumn("age");
   
  // Add some rows to the table
  let newRow = table.addRow();
  newRow.setString("name", "Eren");
  newRow.setString("age", 21);
   
  newRow = table.addRow();
  newRow.setString("name", "Marco");
  newRow.setString("age", 27);
   
  newRow = table.addRow();
  newRow.setString("name", "Eren");
  newRow.setString("age", 23);
   
  newRow = table.addRow();
  newRow.setString("name", "Mikasa");
  newRow.setString("age", 23);
   
  showTable();
}
   
function getFindResults() {
  clear();
   
  let findQuery = 
      findQueryInput.value();
   
  // Get the row values
  // using findRow()
  if (findQuery != "") {
   
    // Find the result in
    // the column of 'name' 
    findResults =
      table.findRow(findQuery, 'name');
     
    if (findResults) {
      text("The row that matches the query is",
           20, 120);
   
      // Display the matched value
      text(findResults.arr[0], 20, 140);
      text(findResults.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);
}

输出:
findRow-ex1

示例 2:

function setup() {
  createCanvas(500, 300);
  textSize(16);
   
  findQueryInput = createInput();
  findQueryInput.position(30, 40);
   
  getColBtn =
    createButton("Get the matching row");
  getColBtn.position(30, 70);
  getColBtn.mouseClicked(getFindResults);
   
  // Create the table
  table = new p5.Table();
   
  // Add two columns
  table.addColumn("name");
  table.addColumn("age");
   
  // Add some rows to the table
  let newRow = table.addRow();
  newRow.setString("name", "Sam");
  newRow.setString("age", 23);
   
  newRow = table.addRow();
  newRow.setString("name", "Max");
  newRow.setString("age", 20);
   
  newRow = table.addRow();
  newRow.setString("name", "Ben");
  newRow.setString("age", 17);
   
  newRow = table.addRow();
  newRow.setString("name", "Sam");
  newRow.setString("age", 23);
   
  showTable();
}
   
function getFindResults() {
  clear();
   
  let findQuery = findQueryInput.value();
   
  // Get the row values
  // using findRow()
  if (findQuery != "") {
   
    // Find the result in the
    // column with ID of '1'
    findResults
      = table.findRow(findQuery, 1);
       
    if (findResults) {
      text("The row that matches the query is",
           20, 120);
   
      // Display the matched value
      text(findResults.arr[0], 20, 140);
      text(findResults.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 'age' 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 'age' column table",
           20, 20);
}

输出:
findRow-ex2

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

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

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