Open In App

p5.js subset() function

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Array: This parameter holds the array elements on which the subset() operation is to be done to get its subset element.
  • Start: This is the number from where extraction is to be done. Its value starts from 0.
  • Count: This is the number of elements that are to be extracted.

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. 

javascript




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. 

javascript




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



Last Updated : 22 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads