Wednesday, July 3, 2024
HomeLanguagesJavascriptD3.js rollups() method

D3.js rollups() method

With the help of d3.rollups() method, we can get the reduced nested array instead of map from iterable data structure having key and values.

Syntax:

 d3.rollups(iterable, reduce, ...keys)

Return value: It will return the reduced nested array instead of map.

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

npm install d3

Example 1: In this example, we can see that by using d3.rollups() method we are able to get the reduced nested array instead of nested map from an iterable data structure having key and values.

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: "ABC", amount: "34.05",  date: "01/04/2016"}
]
  
var gfg = d3.rollups(data, g => g.length, d => d.name);
  
console.log(gfg);


Output:

[ [ 'ABC', 2 ], [ 'DEF', 1 ], [ 'MNO', 1 ] ]

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: "DEF", amount: "34.05",  date: "03/04/2020"}
]
  
var gfg = d3.rollups(data, g => g.length, d => d.name, d => d.date);
  
console.log(gfg);


Output:

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

Last Updated :
14 Sep, 2020
Like Article
Save Article

Similar Reads
Related Tutorials
Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments