Friday, September 19, 2025
HomeLanguagesJavascriptD3.js indexes() method

D3.js indexes() method

With the help of d3.indexes() method, we can get the nested array instead of nested map having key as element. We get an object as unique values instead of an array by using this method.

Syntax :

d3.indexes(iterable, ...keys)

Return value: It returns the nested array instead of nested map.

Note: To execute the below examples you have to install the d3 library by using this command prompt we have to execute the following command.

npm install d3

Example 1: In this example we can see that by using d3.indexes() method, we are able to get the nested array instead of nested map having key as element and value as object of unique variables.

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);


Output:

 [ [ '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' } ] ]

Example 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);


Output:

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

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11837 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7054 POSTS0 COMMENTS
Thapelo Manthata
6738 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS