Open In App

D3.js count() method

Last Updated : 06 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
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

Article Tags :

Similar Reads