p5.js Table getRows() Method

The getRows() method of p5.Table in p5.js is used to return a reference to all rows in the table as an array of p5.TableRow objects. Each of the returned row objects can be used to get and set values as required.

Syntax:

getRows()

Parameters: This function does not accept any paramters.

Below example illustrates the getRows() function in p5.js:

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
    createCanvas(500, 400);
    textSize(16);
  
    text("Click on the button to display" +
        "all the rows in the table", 20, 20);
  
    getColBtn = createButton("Show all rows");
    getColBtn.position(30, 50);
    getColBtn.mouseClicked(showAllRows);
  
    // Create the table
    table = new p5.Table();
  
    // Add two columns
    table.addColumn("movie");
    table.addColumn("rating");
    table.addColumn("price");
  
    // Add 10 randomly generated rows
    for (let i = 0; i < 10; i++) {
        let newRow = table.addRow();
        newRow.setString("movie",
            "Movie " + floor(random(1, 100)));
        newRow.setString("rating",
            floor(random(1, 5)));
        newRow.setString("price",
            "$" + floor(random(10, 100)));
    }
}
  
function showAllRows() {
    clear();
  
    let currentRows = table.getRows();
  
    // Display the total rows
    // present in the table
    text("There are " +
        currentRows.length +
        " rows in the table", 20, 100);
  
    for (let r = 0; r < currentRows.length; r++)
        text(currentRows[r].arr.toString(),
            20, 140 + r * 20);
  
    text("Click on the button to display" +
        "all the rows in the table", 20, 20);
}

chevron_right


Output:
getRows-ex

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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.