Open In App

D3.js | d3.pairs() function

Last Updated : 28 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The d3.pairs() function in D3.js is used to create pair of array elements from the given array elements. If the given array is having less than two elements then it returns the empty array.

Syntax:

d3.pairs(Array)

Parameters: This function accepts a parameter Array whose elements are going to be paired.

Return Value: It returns the array of paired elements.

Below programs illustrate the d3.pairs() function in D3.js.

Example 1:




<html>
  
<head>
    <title>
      Getting pair of two elements 
      from the elements of given array
  </title>
</head>
  
<body>
    <script src='https://d3js.org/d3.v4.min.js'>
  </script>
  
    <script>
        // initialising the arrays of elements
        var Array1 = [10, 20, 30, 40];
        var Array2 = [1, 2, 3, 4];
  
        // Calling to d3.pairs() function
        A = d3.pairs(Array1);
        B = d3.pairs(Array2);
  
        // Getting pair of two elements from 
        // the elements of given array
        document.write(A + "<br>");
        document.write(B + "<br>");
    </script>
</body>
  
</html>


Output:

[[10, 20], [20, 30], [30, 40]]
[[1, 2], [2, 3], [3, 4]]

Example 2:




<html>
  
<head>
    <title>
      Getting pair of two elements
      from the elements of given array
  </title>
</head>
  
<body>
    <script src='https://d3js.org/d3.v4.min.js'>
  </script>
  
    <script>
        // initialising the arrays of elements
        var Array1 = ["A", "B", "C"];
        var Array2 = ["a", "b"];
  
        // Calling to d3.pairs() function
        A = d3.pairs(Array1);
        B = d3.pairs(Array2);
  
        // Getting pair of two elements from
        // the elements of given array
        document.write(A + "<br>");
        document.write(B + "<br>");
    </script>
</body>
  
</html>


Output:

[[A, B], [B, C]]
[a, b]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads