Open In App

D3.js zip() Function

Last Updated : 10 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The zip() function in D3.js is used to form a matrix from an array given as a parameter. It helps to visualize data. D3.js stands for Data-Driven Documents.

Syntax:

d3.zip(arrays…)

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

  • arrays:  This parameter holds the series of arrays.

Return Value: It returns the array of arrays.

Below examples illustrate the zip() function in D3.js:

Example 1:

HTML
<!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>
<body>
  <!--fetching from CDN of D3.js-->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    console.log("Making matrix using zip function: ")
    let m=d3.zip(["aa","ab","ac"],
                 ["ac","ad","ah"],["ae","af","ag"]);
    console.log("Matrix returned by zip function is: ",m)
  </script>
</body>
</html>

Output:

Example 2: When nothing is given in the parameter an empty array is returned by the zip function.

HTML
<!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>
<body>
  <!--fetching from CDN of D3.js -->
  <script type = "text/javascript" 
          src = "https://d3js.org/d3.v4.min.js">
  </script>
  <script>
    let m=d3.zip();
    console.log("Type of m is: ",typeof(m))
    console.log("Matrix returned by zip function is: ",m)
  </script>
</body>
</html>

Output:


Next Article

Similar Reads