Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js Array.from() Method

  • Last Updated : 14 Sep, 2020

With the help of Array.from() method, we can convert the map into an array by using this method.

Syntax :

Array.from( map )

Return value: It returns an array of different elements.

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

npm install d3

Example 1: In this example we can see that by using Array.from() method, we are able to get the array from map after conversion.

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

Output:

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

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

Output:

[ [ 'ABC', Map { '11/12/2019' => [Array] } ],
  [ 'DEF', Map { '11/02/2020' => [Array] } ],
  [ 'MNO', Map { '01/04/2020' => [Array] } ],
  [ 'XYZ', Map { '03/04/2020' => [Array] } ] ]

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!