当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。