p5.js |拼接()函数
p5.js 中的splice()函数用于在给定数组中插入值或数组元素。
句法:
splice(Array, Values, Position)
参数:此函数接受三个参数,如上所述,如下所述:
- 数组:这是要修改的给定数组。
- 值:这是值或另一个数组,其元素要插入到要修改的给定数组中。
- 位置:这是给定数组的元素数,在此之后插入新值或另一个数组的元素。
返回值:返回一个新的修改后的数组。
下面的程序说明了 p5.js 中的 splice()函数:
示例 1:此示例使用 splice()函数在给定数组中插入值或数组元素。
javascript
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);
}
javascript
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);
}
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 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);
}
输出:
示例 2:此示例使用 splice()函数在给定数组中插入值或数组元素。
javascript
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);
}
输出:
示例 3:此示例使用 splice()函数将数组元素插入另一个数组。
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 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);
}
输出:
参考: https://p5js.org/reference/#/p5/splice