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


d3.js d3.set.clear()用法及代碼示例


D3.js中的set.clear()函數用於從集中刪除所有值。它清除該集並保留該空白集,您可以在第二個示例中進行檢查。

用法:

d3.set().clear();

參數:該函數不接受任何參數。


返回值:此函數不返回任何值。

以下程序說明了D3.js中的d3.set.clear()函數:

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> d3.set.clear() Function</title> 
  
    <script src='https://d3js.org/d3.v4.min.js'></script> 
</head> 
  
<body> 
    <script> 
  
        // Initialising the set 
        var set = d3.set(["1", "2", "a", "b", "Geeks"]); 
  
        // Checking the set 
        console.log(set); 
        // Calling the set.clear() function 
        set.clear(); 
  
        // Checking whether any element is present 
        // in the set or not 
        A = set.has("a"); 
        B = set.has("Geeks"); 
  
        // Getting the output of true or false 
        console.log(A); 
        console.log(B); 
    </script> 
</body> 
  
</html>

輸出:

{"$1":"1","$2":"2","$a":"a","$b":"b","$Geeks":"Geeks"}
  false
  false

示例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> d3.set.clear() Function</title> 
  
    <script src='https://d3js.org/d3.v4.min.js'></script> 
</head> 
  
<body> 
  <script> 
      
     // Initialising the set 
     var set = d3.set(["1", "2", "a", "b", "Geeks"]); 
       
     // Checking whether any element is present 
     // in the set or not before calling set.clear() function 
     A = set.has("a"); 
     B = set.has("Geeks"); 
       
     // Getting the output of true or false 
     console.log(A); 
     console.log(B); 
      
     // Calling the set.clear() function 
     set.clear(); 
      
     // Checking whether any element is present 
     // in the set or not after calling set.clear() function 
     C = set.has("a"); 
     D = set.has("Geeks"); 
       
     // Getting the output of true or false 
     console.log(C); 
     console.log(D); 
      
  </script> 
</body> 
  
</html>

輸出:

true
true
false
false

參考: https://devdocs.io/d3~5/d3-collection#set_clear



相關用法


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