Skip to content
Related Articles

Related Articles

Improve Article

D3.js nest.entries() function

  • Last Updated : 29 Jul, 2020

The nest.entries() function in D3.js is used to generate the nest and is applies a nest operator to every level of hierarchy rather than the outmost layer only.

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.entries(array)

Parameters: It takes only one parameter given above and described below.

  • Array: It is the array of objects.

Return Value: It returns the array of key-value entries.



Below given are a few examples of the above function.

Example 1: When no entries function is used and keys are assigned.




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width,
                 initial-scale=1.0">
  <title>Document</title>
</head>
<style>
</style>
<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=[
      {val:"val10", data:"data11"},
      {val:"val20", data:"data22"},
      {val:"val30", data:"data33"},
      {val:"val30", data:"data33"},
      {val:"val30", data:"data33"},
      {val:"val50", data:"data33"},
      {val:"val40", data:"data44"}
    ]
    let data= d3.nest()
                  .key(function(d) { return d.val; })
    console.log(data);
  </script>
</body>
</html>

Output:

Example 2: When entries function is used with the nest.




<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width,
                       initial-scale=1.0">
  <title>Document</title>
</head>
<style>
</style>
<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=[
      {val:"val10", data:"data11"},
      {val:"val20", data:"data22"},
      {val:"val30", data:"data33"},
      {val:"val30", data:"data33"},
      {val:"val30", data:"data33"},
      {val:"val50", data:"data33"},
      {val:"val40", data:"data44"}
    ]
    let data= d3.nest()
                  .key(function(d) { return d.val; })
                  .entries(array)
    console.log("Type is: ", typeof array)
    console.log(data);
  </script>
</body>
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :