Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

D3.js nest.object() Function

  • Last Updated : 29 Jul, 2020

The nest.object() function in D3.js is used to apply the nest operator to the given array and returns the nested object of key-value pair.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

nest.object( array )

Parameters: This function accepts single parameter as mentioned above and described below:

  • Array: This parameter holds the array of objects.

Return Value: It returns the object.



Below are a few examples of the above function

Example 1: When no duplicate key exists.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <style>
        .originalColor {
            height: 100px;
            width: 100px;
        }
  
        .darkerColor {
            height: 100px;
            width: 100px;
        }
    </style>
</head>
  
<body>
  
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
        // Forming the array of objects
        let array = [
            { car: "car1" }, { model: "model1" },
            { car: "car2" }, { model: "model1" },
            { car: "car3" }, { model: "model2" },
            { car: "car4" }, { model: "model2" },
            { car: "car5" }, { model: "model3" }
        ]
        let data = d3.nest()
            .key(function (d) { return d.car; })
            .key(function (d) { return d.model; })
            .object(array)
        console.log("Type is: ", typeof array)
        console.log(data);
    </script>
</body>
  
</html>

Output:

Example 2: When duplicate keys exist and accessing those keys that does not exist when accessed, outputs undefined.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <style>
        .originalColor {
            height: 100px;
            width: 100px;
        }
  
        .darkerColor {
            height: 100px;
            width: 100px;
        }
    </style>
</head>
  
<body>
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
        // Forming the array of objects
        let array = [
            { car: "car1" }, { model: "model1" },
            { car: "car3" }, { model: "model1" },
            { car: "car3" }, { model: "model2" },
            { car: "car3" }, { model: "model2" },
            { car: "car5" }, { model: "model3" }
        ]
        let data = d3.nest()
            .key(function (d) { return d.car; })
            .key(function (d) { return d.model; })
            .key(function (d) { return d.car; })
            .object(array)
        console.log(data);
        console.log(data.car1);
  
        // Key does not exists so 
        // output is undefined
        console.log(data.car2);
        console.log(data.car3);
    </script>
</body>
  
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :