D3.js Array.from() 方法
借助Array.from()方法,我们可以使用该方法将地图转换为数组。
句法 :
Array.from( map )
返回值:它返回一个不同元素的数组。
注意:要执行以下示例,您必须使用命令提示符安装d3库,我们必须执行以下命令。
npm install d3
例子1:在这个例子中我们可以看到,通过使用Array.from()方法,我们可以从map中得到转换后的数组。
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));
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));
输出:
[ [ 'ABC', [ [Object] ] ],
[ 'DEF', [ [Object] ] ],
[ 'MNO', [ [Object] ] ],
[ 'XYZ', [ [Object] ] ] ]
示例 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));
输出:
[ [ 'ABC', Map { '11/12/2019' => [Array] } ],
[ 'DEF', Map { '11/02/2020' => [Array] } ],
[ 'MNO', Map { '01/04/2020' => [Array] } ],
[ 'XYZ', Map { '03/04/2020' => [Array] } ] ]