📜  D3.js group.get() 方法

📅  最后修改于: 2022-05-13 01:56:26.824000             🧑  作者: Mango

D3.js group.get() 方法

借助d3.group.get()方法,我们可以从通过对可迭代数据结构分组获得的结果映射中获取值。

句法:

d3.group.get( value )

返回值:它返回地图中的值。

注意:要执行以下示例,您必须使用此命令提示符安装 d3 库,我们必须执行以下命令。

npm install d3

示例 1:在此示例中,我们可以看到通过使用d3.group.get()方法,我们能够从从组中获取的结果映射中获取值。

Javascript
// Defining d3 contrib variable  
var d3 = require('d3');
  
data = [
  {name: "ABC", amount: "34.0",   date: "11/12/2015"},
  {name: "DEF", amount: "120.11", date: "11/12/2015"},
  {name: "MNO", amount: "12.01",  date: "01/04/2016"},
  {name: "XYZ", amount: "34.05",  date: "01/04/2016"}
]
  
var grouped_data = d3.group(data, d => d.name)
  
console.log(grouped_data.get("ABC"))


Javascript
// Defining d3 contrib variable  
var d3 = require('d3');
  
data = [
  {name: "ABC", amount: "34.0",   date: "11/12/2019"},
  {name: "DEF", amount: "120.11", date: "11/02/2020"},
  {name: "MNO", amount: "12.01",  date: "01/04/2020"},
  {name: "XYZ", amount: "34.05",  date: "03/04/2020"}
]
  
var grouped_data = d3.group(data, d => d.name, d => d.date)
  
console.log(grouped_data.get("XYZ"))


输出:

[ { name: 'ABC', amount: '34.0', date: '11/12/2015' } ]

示例 2:

Javascript

// Defining d3 contrib variable  
var d3 = require('d3');
  
data = [
  {name: "ABC", amount: "34.0",   date: "11/12/2019"},
  {name: "DEF", amount: "120.11", date: "11/02/2020"},
  {name: "MNO", amount: "12.01",  date: "01/04/2020"},
  {name: "XYZ", amount: "34.05",  date: "03/04/2020"}
]
  
var grouped_data = d3.group(data, d => d.name, d => d.date)
  
console.log(grouped_data.get("XYZ"))

输出:

Map {
  '03/04/2020' => [ 
    { name: 'XYZ', amount: '34.05', date: '03/04/2020' } ]
}