Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptp5.js Table removeColumn() Method

p5.js Table removeColumn() Method

The removeColumn() method of p5.Table in p5.js is used to remove the given column from a table. The column to remove can be specified by either using its title or its index value (column ID).

Syntax:

removeColumn( column )

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

  • column: It is a String or Number that denotes the name of the column or the column ID to remove.

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

Example 1:




function setup() {
    createCanvas(500, 400);
    textSize(16);
  
    removeColBtn =
        createButton("Clear Last Column");
  
    removeColBtn.position(30, 60);
    removeColBtn.mouseClicked(clearLastColumn);
  
    // Create the table
    table = new p5.Table();
  
    // Add columns
    table.addColumn("author");
    table.addColumn("book");
    table.addColumn("price");
    table.addColumn("rating");
  
    // Add 10 random rows to the table
    for (let i = 0; i < 10; i++) {
        let newRow = table.addRow();
        newRow.setString("author",
            "Author " + floor(random(1, 100)));
        newRow.setString("book",
            "Book " + floor(random(1, 100)));
        newRow.setString("price",
            "$" + floor(random(10, 100)));
        newRow.setString("rating",
            random(1, 5).toFixed(2));
    }
  
    // Display the rows currently present
    getTableRows();
}
  
function clearLastColumn() {
    clear();
    text("Click on the button to clear "
        + "the last column in the table",
        20, 20);
  
    // Get the index of the last column
    // of the table
    let lastColumn =
        table.getColumnCount() - 1;
  
    // Use the removeColumn() method to
    // clear the given column of the table
    if (lastColumn >= 0)
        table.removeColumn(lastColumn);
  
    text("Last column cleared!", 20, 140);
    getTableRows();
}
  
function getTableRows() {
    clear();
    text("Click on the button to clear "
        + "the last column in the table",
        20, 20);
  
    // Display the total columns
    // present in the table
    text("There are " +
        table.getColumnCount() +
        " columns in the table", 20, 120);
  
    // Show the table with the columns and rows
    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);
}


Output:
removeCol-last

Example 2:




function setup() {
  createCanvas(500, 400);
  textSize(16);
   
  columnInput = createInput();
  columnInput.position(30, 40);
   
  clearColBtn =
    createButton("Clear Given Column");
  clearColBtn.position(30, 70);
  clearColBtn.mouseClicked(clearColumn);
   
  // Create the table
  table = new p5.Table();
   
  // Add columns
  table.addColumn("author");
  table.addColumn("book");
  table.addColumn("price");
  table.addColumn("rating");
   
  // Add 10 random rows to the table
  for (let i = 0; i < 10; i++) {
    let newRow = table.addRow();
    newRow.setString("author",
      "Author " + floor(random(1, 100)));
    newRow.setString("book",
      "Book " + floor(random(1, 100)));
    newRow.setString("price",
      "$" + floor(random(10, 100)));
    newRow.setString("rating",
       random(1, 5).toFixed(2));
  }
   
  // Display the rows currently present
  getTableRows();
}
   
function clearColumn() {
  clear();
  text("Click on the button to clear "
     + "the given column in the table",
    20, 20);
   
  // Get the index of the column to be removed
  let colToClear =
    int(columnInput.value());
   
  // Use the removeColumn() method to
  // clear the given column of the table
  if (colToClear >= 0 &&
      colToClear < table.getColumnCount())
    table.removeColumn(colToClear);
   
  text("Last column cleared!", 20, 140);
  getTableRows();
}
   
function getTableRows() {
  clear();
  text("Click on the button to clear "
    + "the given column in the table",
    20, 20);
   
  // Display the total columns 
  // present in the table
  text("There are "
    table.getColumnCount() + 
    " columns 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);
}


Output:
removeCol-specified

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments