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


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

p5.j​​s中p5.Table的getArray()方法用於將表中的所有數據作為多維數組檢索。可以迭代該數組以獲取表中的所有值。

用法:

getArray()

參數:此方法不接受任何參數。

返回值:此方法返回包含表中所有數據的多維數組。

下麵的示例說明了p5.js中的getArray()方法:



例:

Javascript

function setup() { 
  createCanvas(600, 300); 
  textSize(18); 
  
  text("Click on the button to get the " + 
       "values of the table as an array", 
       20, 20); 
  
  setBtn = 
    createButton("Get all table values"); 
  setBtn.position(30, 40); 
  setBtn.mouseClicked(showTable); 
  
  // Create the table 
  table = new p5.Table(); 
  
  setTableData(); 
} 
  
function setTableData() { 
  table.addColumn('Invention'); 
  table.addColumn('Inventors'); 
  
  let tableRow = table.addRow(); 
  tableRow.setString('Invention', 'Telescope'); 
  tableRow.setString('Inventors', 'Galileo'); 
  
  tableRow = table.addRow(); 
  tableRow.setString('Invention', 'Steam Engine'); 
  tableRow.setString('Inventors', 'James Watt'); 
  
  tableRow = table.addRow(); 
  tableRow.setString('Invention', 'Radio'); 
  tableRow.setString('Inventors', 'Guglielmo Marconi'); 
} 
  
function showTable() { 
  clear(); 
  text("All values of the table are retrieved " + 
       "using the getArray() method", 20, 20); 
  
  // Get all the values in the table as an array 
  let tableArray = table.getArray(); 
  console.table(tableArray); 
  
  // Show all the rows currently 
  // present in the multi-dimensional array 
  for (let r = 0; r < tableArray.length; r++) { 
    for (let c = 0; c < tableArray[0].length; c++) { 
      text(str(tableArray[r]), 
           20 + 160 * c, 100 + 20 * r); 
    } 
  } 
}

輸出:

控製台輸出:

在線編輯: https://editor.p5js.org/

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

參考: https://p5js.org/reference/#/p5.Table/getArray

相關用法


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