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


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


D3.js中的d3.entries()函數用於返回包含指定對象的屬性名稱和屬性值的數組或關聯數組。

用法:

d3.entries(object)

參數:此函數接受成對包含鍵和值的單個參數對象。


返回值:它返回一個包含指定對象的屬性名稱和值的數組或一個關聯數組。

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

範例1:

<!DOCTYPE html>  
<html>  
      
<head>  
    <title> d3.entries() function</title>  
          
    <script src='https://d3js.org/d3.v4.min.js'></script>  
</head>  
<body>  
      
<script> 
       
     // Initialising an object 
     var month = {"January":1, "February":2, "March":3}; 
            
     // Calling the d3.entries() function 
     A = d3.entries(month); 
        
     // Getting the key and value in pairs 
     console.log(A); 
  </script> 
</body>  
</html>

輸出:

[{"key":"January","value":1},{"key":"February","value":2},
                                       {"key":"March","value":3}]

範例2:

<!DOCTYPE html>  
<html>  
      
<head>  
    <title> d3.entries() function</title>  
          
    <script src='https://d3js.org/d3.v4.min.js'></script>  
</head>  
<body>  
  <script src = "https://d3js.org/d3.v4.min.js"></script> 
  <script> 
       
     // Initialising an object 
     var month = {"GeeksforGeeks":0, "Geeks":2, 
                                     "Geek":3, "gfg":4}; 
            
     // Calling the d3.entries() function 
     A = d3.entries(month); 
        
     // Getting the key and value in pairs. 
     console.log(A); 
  </script> 
</body>  
</html>

輸出:

[{"key":"GeeksforGeeks","value":0},{"key":"Geeks","value":2},
                     {"key":"Geek","value":3},{"key":"gfg","value":4}]

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



相關用法


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