p5.js中的splice()函數用於在給定數組中插入值或數組元素。
用法:
sort(Array, Values, Position)
參數:此函數接受上述和以下所述的三個參數:
- Array:這是一個要修改的給定數組。
- Values:這是值或另一個數組,其元素將被插入到要修改的給定數組中。
- Position:這是給定數組的元素數,之後將插入新值或另一個數組的元素。
返回值:它返回一個新的修改後的數組。
以下程序說明了p5.js中的splice()函數:
示例1:本示例使用splice()函數在給定數組中插入值或數組元素。
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);
}
輸出:
示例2:本示例使用splice()函數在給定數組中插入值或數組元素。
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()函數將數組元素插入另一個數組。
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
相關用法
- Javascript Array.splice()用法及代碼示例
- p5.js sin()用法及代碼示例
- p5.js red()用法及代碼示例
- PHP pow( )用法及代碼示例
- p5.js hex()用法及代碼示例
- p5.js nfc()用法及代碼示例
- p5.js nfp()用法及代碼示例
- p5.js nfs()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js log()用法及代碼示例
- d3.js d3.sum()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | splice() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。