Open In App

D3.js count() method

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

With the help of d3.count() method, we can get the count of valid values in the specified iterable data structure.

Syntax:

d3.count(iterable[, accessor])

Return value: It returns the count of valid values.

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.count() method, we are able to get the count of valid values in the specified iterables.

Javascript




// Defining d3 contrib variable
const 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" }
]
 
let gfg = d3.count(data, d => d.amount);
console.log(gfg);


Output:

4

Example 2:

Javascript




// Defining d3 contrib variable
const 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 }
]
 
let gfg = d3.count(data, d => d.age);
console.log(gfg);


Output :

3


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads