Open In App

p5.js subset() function

The subset() function in p5.js is used to get the subset of the given array elements. This function extracts the elements from an existing array. 

Syntax:



subset(Array, Start, Count)

Parameters: This function accepts three parameters as mentioned above and described below:

Return Value: It returns the extracted elements. The below programs illustrate the subset() function in p5.js



Example 1: This example uses the subset() function to get the subset of the given array of elements. 




function setup() {
  
    // Creating Canvas size
    createCanvas(600, 90);
}
  
function draw() {
  
    // Set the background color 
    background(220);
  
    // Initializing the array
    let Array = ['Ram', 'Geeta', 'Shita', 'Shyam'];
  
    // Initializing a start value from where
    // extraction is done
    // it starts from 0.
    let Start = 1;
  
    // Initializing the count which defines
    // the number of elements to be extracted 
    let Count = 2;
  
    // Calling to subset() function.
    let A = subset(Array, Start, Count);
  
    // Set the size of text 
    textSize(16);
  
    // Set the text color 
    fill(color('red'));
  
    // Getting extracted elements
    text(& quot;Extracted elements are: & quot; + A, 50, 30);
}

Output: 

Example 2: This example uses the subset() function to get the subset of the given array elements. 




function setup() {
  
    // Creating Canvas size
    createCanvas(600, 90);
}
  
function draw() {
  
    // Set the background color 
    background(220);
  
    // Initializing the array
    let Array = ['Ram', 'Geeta', 'Shita', 'Shyam'];
  
    // Initializing a start value from where
    // extraction is done
    // it starts from 0.
    let Start = 0;
  
    // Initializing the count which defines
    // the number of elements to be extracted 
    let Count = 3;
  
    // Calling to subset() function.
    let A = subset(Array, Start, Count);
  
    // Set the size of text 
    textSize(16);
  
    // Set the text color 
    fill(color('red'));
  
    // Getting extracted elements
    text(& quot;Extracted elements are: & quot; + A, 50, 30);
}

Output: 

Reference: https://p5js.org/reference/#/p5/subset


Article Tags :