With the help of d3.cross() method, we can get the Cartesian product of iterables specified in the parameters by using this method.
Syntax :
d3.cross(...iterables[, reducer])
Return Value :
It will return the cartesian product.
Note : To execute the below examples you have to install the d3 library by using this command prompt we have to execute the following command.
npm install d3
Example #1 : In this example we can see that by using d3.cross() method, we are able to get the cartesian product of specified iterables by using this method.
// Defining d3 contrib variable
var d3 = require('d3');
var result = d3.cross(["A", "B", "C"], ["X", "Y", "Z"]); console.log(result);
Output :
[ [ 'A', 'X' ], [ 'A', 'Y' ], [ 'A', 'Z' ], [ 'B', 'X' ], [ 'B', 'Y' ], [ 'B', 'Z' ], [ 'C', 'X' ], [ 'C', 'Y' ], [ 'C', 'Z' ] ]
Example #2 :
// Defining d3 contrib variable
var d3 = require('d3');
var result = d3.cross(["A", "B", "C"], ["X", "Y", "Z"], (a, b) => a + b);
console.log(result);
Output :
[ 'AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ' ]