Open In App

D3.js d3.cross() function

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The d3.cross() function in D3.js is used to return the cartesian product of the given two arrays A and B.

Syntax:

d3.cross(Array1, Array2)

Parameters:

This function accepts two parameters which are mentioned above and described below:

  • Array1: This is the first array whose elements are going to be performed cartesian product with array2.
  • Array2: This is the second array whose elements are going to be performed cartesian product with array1.

Return Value:

It returns the cartesian product array of two-element each of two given array.

Example 1: This example shows the use of cross() function.

HTML
<!DOCTYPE html>

<html>

<head>
    <title>
        Getting cartesian product
        of the two given array
    </title>
</head>

<body>
    <script src='https://d3js.org/d3.v4.min.js'>
    </script>

    <script>
        // initialising the array of elements
        let Array1 = [10, 20, 30];
        let Array2 = [1, 2, 3];

        // Calling to d3.cross() function
        A = d3.cross(Array1, Array2);

        // Getting cartesian product of the two given array
        document.write(A + "<br>");
    </script>
</body>

</html>

Output:

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

Example 2: This example shows the use of cross() function.

HTML
<!DOCTYPE html>

<html>

<head>
    <title>
        Getting cartesian product
        of the two given array
    </title>
</head>

<body>
    <script src='https://d3js.org/d3.v4.min.js'>
    </script>

    <script>
        // initialising the array of elements
        let Array1 = ["A", "B", "C"];
        let Array2 = ["a", "b", "c"];

        // Calling to d3.cross() function
        A = d3.cross(Array1, Array2);

        // Getting cartesian product of the two given array
        document.write(A + "<br>");
    </script>
</body>

</html>

Output:

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


Next Article

Similar Reads