Open In App

p5.js splice() function

The splice() function in p5.js is used to insert values or array elements in the given array.
Syntax: 
 

splice(Array, Values, Position)

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



Return Value: It returns a new modified array.
Below programs illustrate the splice() function in p5.js:
Example 1: This example uses splice() function to insert values or array elements in the given array. 
 




function setup() { 
  
    // Creating Canvas size
    createCanvas(500, 90); 
  
function draw() { 
      
    // Set the background color 
    background(220); 
      
    // Initializing the array
    let Array = [9, 6, 0, 22, 4, 1, 15];
    
    // Initializing the value which are
    // to be inserted
    let Value = 5;
    
    // Initializing the position after 
    // which insertion is done 
    // counting is done from starting
    let Position = 5;
    
    // Calling to splice() function.
    let A = splice(Array, Value, Position);
      
    // Set the size of text 
    textSize(16); 
      
    // Set the text color 
    fill(color('red')); 
    
    // Getting new modified array
    text("New modified array is : " + A, 50, 30);           

Output: 
 



Example 2: This example uses splice() function to insert values or array elements in the given array. 
 




function setup() { 
  
    // Creating Canvas size
    createCanvas(500, 90); 
  
function draw() { 
      
    // Set the background color 
    background(220); 
      
    // Initializing the array
    let Array = ['Ram', 'Geeta', 'Shita', 'Shyam'];
    
    // Initializing the value which are
    // to be inserted
    let Value = 'Geeks';
    
    // Initializing the position after 
    // which insertion is done 
    // counting is done from starting
    let Position = 2;
    
    // Calling to splice() function.
    let A = splice(Array, Value, Position);
      
    // Set the size of text 
    textSize(16); 
      
    // Set the text color 
    fill(color('red')); 
    
    // Getting new modified array
    text("New modified array is : " + A, 50, 30);           

Output: 
 

Example 3: This example uses splice() function to insert array elements into another array. 
 




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 new array whose elements are
    // to be inserted
    let Value = ['Geeks', 'GeeksforGeek'];
    
    // Initializing the position after 
    // which insertion is done 
    // counting is done from starting
    let Position = 2;
    
    // Calling to splice() function.
    let A = splice(Array, Value, Position);
      
    // Set the size of text 
    textSize(16); 
      
    // Set the text color 
    fill(color('red')); 
    
    // Getting new modified array
    text("New modified array is : " + A, 50, 30);
               

Output: 
 

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


Article Tags :