D3.js index() 方法
在d3.index()方法的帮助下,我们可以获得以键为元素的地图,但变化是我们将对象作为唯一值而不是数组。
句法:
d3.index(iterable, ...keys)
返回值:返回对象的映射。
注意:要执行以下示例,您必须使用带有以下命令的命令提示符安装d3库。
npm install d3
示例 1:在这个示例中,我们可以看到,通过使用d3.index()方法,我们能够获得以键为元素、以值为对象的地图。
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 gfg = d3.index(data, d => d.name);
console.log(gfg);
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 gfg = d3.index(data, d => d.name, d => d.date);
console.log(gfg);
输出:
Map {
'ABC' => { name: 'ABC', amount: '34.0', date: '11/12/2015' },
'DEF' => { name: 'DEF', amount: '120.11', date: '11/12/2015' },
'MNO' => { name: 'MNO', amount: '12.01', date: '01/04/2016' },
'XYZ' => { name: 'XYZ', amount: '34.05', date: '01/04/2016' } }
示例 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 gfg = d3.index(data, d => d.name, d => d.date);
console.log(gfg);
输出:
Map {
'ABC' => Map {
'11/12/2019' => { name: 'ABC', amount: '34.0', date: '11/12/2019' } },
'DEF' => Map {
'11/02/2020' => { name: 'DEF', amount: '120.11', date: '11/02/2020' } },
'MNO' => Map {
'01/04/2020' => { name: 'MNO', amount: '12.01', date: '01/04/2020' } },
'XYZ' => Map {
'03/04/2020' => { name: 'XYZ', amount: '34.05', date: '03/04/2020' } } }
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。