D3.js | d3.pairs() function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.