Open In App

p5.Table getArray() Method

Last Updated : 16 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The getArray() method of p5.Table in p5.js is used to retrieve all the data in the table as a multidimensional array. This array can be iterated through to get all the values in the table.

Syntax:

getArray()

Parameters: This method does not accept any parameters.

Return Value: This method returns a multidimensional array that contains all the data of the table.

The example below illustrates the getArray() method in p5.js:

Example:

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


Output:

Console Output:

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5.Table/getArray



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads