Skip to content
Related Articles

Related Articles

p5.Table getObject() Method

Improve Article
Save Article
  • Last Updated : 16 Jul, 2020
Improve Article
Save Article

The getObject() method of p5.Table in p5.js is used to retrieve all the data in the table as an object. An optional column name can be specified to store all the rows of the table with that column name as an attribute.

Syntax:

getObject( [headerColumn] )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • headerColumn: It is a String that denotes the name of the column that should be used as a title for each row object.

Return Value: This method returns an object which contains all the data of the table.

The examples below illustrate the getObject() method in p5.js:

Example 1:

Javascript




function setup() {
  createCanvas(600, 300);
  textSize(18);
  
  text("Click on the button to get " +
       "the values of the table as an object",
       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 retrieved using the " +
       "getObject() method", 20, 20);
  
  // Get all the values in the table as an array
  let tableObject = table.getObject();
  console.log(tableObject);
  
  // Get every row in the table using the length
  // of their keys
  for (let r = 0; r < Object.keys(tableObject).length; r++) {
    
    // Display the row using the JSON format
    text(JSON.stringify(tableObject[r]), 20, 100 + 30 * r);
  }
}


Output:

Example 2:

Javascript




function setup() {
  createCanvas(600, 400);
  textSize(18);
  
  text("Click on the button to get the "
       "values of the table as an object",
       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 the values are retrieved " +
       "using the getObject() method", 20, 20);
  
  text("Below is the object representation " +
       "of the whole table", 20, 80);
  
  // Get all the values in the table as an object
  // with the header column as "Invention"
  let tableObject = table.getObject("Invention");
  console.log(tableObject);
  
  // Display the object using the JSON format
  text(JSON.stringify(tableObject, null, '\t'), 20, 120);
}


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/getObject


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!