D3.js count() 方法
借助d3.count()方法,我们可以获得指定可迭代数据结构中有效值的计数。
句法:
d3.count(iterable[, accessor])
返回值:返回有效值的计数。
注意:要执行以下示例,您必须使用以下命令的命令提示符安装d3库。
npm install d3
示例 1:在此示例中,我们可以看到通过使用d3.count()方法,我们能够获取指定迭代中有效值的计数。
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.count(data, d => d.amount);
console.log(gfg);
Javascript
// Defining d3 contrib variable
var d3 = require('d3');
data = [
{name: "ABC", amount: "34.0", age: 20},
{name: "DEF", amount: "120.11", age: NaN},
{name: "MNO", amount: "12.01", age: 23},
{name: "DEF", amount: "34.05", age: 24}
]
var gfg = d3.count(data, d => d.age);
console.log(gfg);
输出:
4
示例 2:
Javascript
// Defining d3 contrib variable
var d3 = require('d3');
data = [
{name: "ABC", amount: "34.0", age: 20},
{name: "DEF", amount: "120.11", age: NaN},
{name: "MNO", amount: "12.01", age: 23},
{name: "DEF", amount: "34.05", age: 24}
]
var gfg = d3.count(data, d => d.age);
console.log(gfg);
输出 :
3