Sunday, December 14, 2025
HomeLanguagesJavascriptD3.js index() method

D3.js index() method

With the help of d3.index() method, we can get the map having key as an element but the change is that we get object as unique values instead of an array.

Syntax:

d3.index(iterable, ...keys)

Return value: It returns the map of objects.

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

npm install d3

Example 1: In this example we can see that by using d3.index() method, we are able to get the map having key as element and value as object.

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


Output:

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

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


Output:

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' } } }
Last Updated :
14 Sep, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11953 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6951 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS