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:
- Array: This is a given array which is to be modified.
- Values: This is the values or an another array whose elements are to be inserted into the given array which are to modified.
- Position: This is the number of elements of the given array after which new values or elements of another array are to be inserted.
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.
javascript
function setup() {
createCanvas(500, 90);
}
function draw() {
background(220);
let Array = [9, 6, 0, 22, 4, 1, 15];
let Value = 5;
let Position = 5;
let A = splice(Array, Value, Position);
textSize(16);
fill(color( 'red' ));
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.
javascript
function setup() {
createCanvas(500, 90);
}
function draw() {
background(220);
let Array = [ 'Ram' , 'Geeta' , 'Shita' , 'Shyam' ];
let Value = 'Geeks' ;
let Position = 2;
let A = splice(Array, Value, Position);
textSize(16);
fill(color( 'red' ));
text( "New modified array is : " + A, 50, 30);
}
|
Output:
Example 3: This example uses splice() function to insert array elements into another array.
javascript
function setup() {
createCanvas(600, 90);
}
function draw() {
background(220);
let Array = [ 'Ram' , 'Geeta' , 'Shita' , 'Shyam' ];
let Value = [ 'Geeks' , 'GeeksforGeek' ];
let Position = 2;
let A = splice(Array, Value, Position);
textSize(16);
fill(color( 'red' ));
text( "New modified array is : " + A, 50, 30);
}
|
Output:
Reference: https://p5js.org/reference/#/p5/splice
Last Updated :
22 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...