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


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


D3.js中的map.clear()函數用於從創建的Map中刪除所有條目。

用法:

d3.map.clear();

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


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

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

示例1:

<!DOCTYPE html> 
<html> 
    
<head> 
    <title> d3.map.clear() Function</title> 
    
    <script src='https://d3js.org/d3.v4.min.js'></script> 
</head> 
  
<body> 
  <script> 
       
     // Creating a map 
     var map = d3.map({"a": 1}, {"b": 2}, {"c": 3}); 
   
     // Calling the map.clear() function 
     map.clear(); 
        
     // Checking whether the value for the specified key  
     // is present or not 
     A = map.get("a"); 
        
    // Getting the output either value of the specified key 
    // string or undefined if the key is not present 
    console.log(A); 
       
  </script> 
</body> 
  
</html>

輸出:

undefined

示例2:

<!DOCTYPE html> 
<html> 
    
<head> 
    <title> d3.map.clear() Function</title> 
    
    <script src='https://d3js.org/d3.v4.min.js'></script> 
</head> 
  
<body> 
  <script> 
       
     // Constructing a map 
     var map = d3.map({"a": 0}, {"b": 1}, {"c": 2}); 
        
     // Checking whether the value for the specified key 
     // is present or not before calling clear() function 
     A = map.get("a"); 
        
     // Getting the output of value 
     console.log(A); 
       
     // Calling the map.clear() function 
     map.clear(); 
       
     // Checking whether the value for the specified key 
     // is present or not after calling clear() function 
     B = map.get("a"); 
        
     // Getting the output of value 
     console.log(B); 
  </script> 
</body> 
  
</html>

輸出:

0
undefined

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



相關用法


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