Saturday, October 18, 2025
HomeLanguagesJavascriptD3.js nest() Function

D3.js nest() Function

D3.js is a library built with javascript and particularly used for data visualization. D3.nest() function is used to group the data as groupBy clause does in SQL. It groups the data on the basis of keys and values.

Syntax:

d3.nest()

Parameters: This function does not accept any parameters.

Return Value: It returns the object with several properties as entries, keys, values, map, sort.

Example 1: In this example, the data is to be grouped on the basis of key “manufactured” we can group the data on the basis of multiple keys as well.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale=1.0">
  
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
  
    <title>
        D3.js d3.nest() Function
    </title>
</head>
  
<body>
    <script>
        var cars = [
            { name: "car1", manufactured: "1950", model: "s51" },
            { name: "car1", manufactured: "1950", model: "s51" },
            { name: "car1", manufactured: "1951", model: "s50" },
            { name: "car1", manufactured: "1951", model: "s50" },
        ];
        var groupedData = d3.nest()
            .key(function (d) { return d.manufactured; })
            .entries(cars);
        console.log("ArrayData :", groupedData);
        console.log("ArrayData[0] :", groupedData[0]);
        console.log("ArrayData[1] :", groupedData[1]);
    </script>
</body>
  
</html


Output: Data is grouped by the manufacturing year with key 1950 and 1951.

Example 2: This example explains the grouping of data on the basis of multiple keys.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent=
        "width=device-width, initial-scale=1.0">
  
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
  
    <title>
        D3.js d3.nest() Function
    </title>
</head>
  
<body>
    <script>
        var cars = [
            { name: "car1", manufactured: "1950", model: "s51" },
            { name: "car1", manufactured: "1950", model: "s51" },
            { name: "car1", manufactured: "1951", model: "s50" },
            { name: "car1", manufactured: "1951", model: "s50" },
        ];
        var groupedData = d3.nest()
            .key(function (d) { return d.manufactured; })
            .key(function (d) { return d.model; })
            .entries(cars);
        console.log("ArrayData :", groupedData);
        console.log("ArrayData[0] :", groupedData[0]);
        console.log("ArrayData[1] :", groupedData[1]);
    </script>
</body>
  
</html>


Output: Data is first grouped by manufacturing year and then by the model number.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS