Cartesian product of iterables with the d3.cross() method

Updated

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' ]
Comment
Mithlesh Upadhyay Published 4 Dec 2026 · 1 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials

  • Classes and objects in TypeScript with examples

    Class in term of OOps is, it is blueprint for creating objects. TypeScript supports object-oriented programming features like classes, Interfaces, Polymorphism, data-binding etc. Object means this an real…

    3 min read
  • Find common elements of two arrays in JavaScript

    The objective is to find the common elements of the arrays, which means if there are two arrays given as A1 = [5, 6, 2, 3, 4] A2…

    4 min read
  • Web workers in JavaScript for multi-threading

    In many situations, having more than one thread running in a program is beneficial. We really have to start thinking about how our program can do multiple things…

    4 min read
  • URLSearchParams.append() method in Node.js

    The urlSearchParams.append() is an inbuilt application programming interface of class URLSearchParams with in url module which is used to append the associate argument of name to value in…

    2 min read
  • Readable, writable and duplex streams in Node.js

    A stream in Node is simply a sequence of bytes. A stream contains a buffer of bytes, and this buffer has a zero or greater length. In Node,…

    3 min read
  • Strict mode in JavaScript

    For a long time, JavaScript had no compatibility issues because in every new version release old functionality were not changing. But it was not good as if there…

    2 min read