當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js subset()用法及代碼示例

p5.j​​s中的subset()函數用於獲取給定數組元素的子集。此函數從現有數組中提取元素。

用法:

subset(Array, Start, Count)

參數:此函數接受上述和以下所述的三個參數:


  • Array:此參數保存將在其上執行subset()操作的數組元素以獲得其子集元素。
  • Start:這是要從中提取的編號。它的值從0開始。
  • Count:這是要提取的元素數。

返回值:它返回提取的元素。

以下程序說明了p5.js中的subset()函數:

示例1:本示例使用subset()函數獲取給定數組元素的子集。

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 form where 
    // extraction is done 
    // it starts form 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("Extracted elements are : " + A, 50, 30);           
} 

輸出:

示例2:本示例使用subset()函數獲取給定數組元素的子集。

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 form where 
    // extraction is done 
    // it starts form 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("Extracted elements are : " + A, 50, 30);            
} 

輸出:

參考: https://p5js.org/reference/#/p5/subset



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | subset() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。