📜  D3.js 索引()方法

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

D3.js 索引()方法

d3.indexes()方法的帮助下,我们可以得到嵌套数组,而不是以键为元素的嵌套映射。通过使用此方法,我们将对象作为唯一值而不是数组。

句法 :

d3.indexes(iterable, ...keys)

返回值:它返回嵌套数组而不是嵌套映射。

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

npm install d3

示例 1:在这个示例中我们可以看到,通过使用d3.indexes()方法,我们能够获得嵌套数组,而不是嵌套映射,其中键作为元素,值作为唯一变量的对象。

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.indexes(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.indexes(data, d => d.name, d => d.date);
console.log(gfg);


输出:

[ [ '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.indexes(data, d => d.name, d => d.date);
console.log(gfg);

输出:

[ [ 'ABC', [ [Array] ] ],
  [ 'DEF', [ [Array] ] ],
  [ 'MNO', [ [Array] ] ],
  [ 'XYZ', [ [Array] ] ] ]