Skip to the content.

@tettekete/cartesian-product

Overview

This module returns the Cartesian product.

Features

SYNOPSIS

import { cartesianProduct } from '@tettekete/cartesian-product';

const arrayList = [
		['A', 'B'],
		['C', 'D', 'E'],
	];


// Common usage
for( const combination of cartesianProduct( arrayList ) )
{
	console.log( combination );
}


// Default order sample
console.log( Array.from(cartesianProduct( arrayList )) ); // default order is [0,1,2,...]
// ->
// [
// 	['A', 'C'],
// 	['B', 'C'],
// 	['A', 'D'],
// 	['B', 'D'],
// 	['A', 'E'],
// 	['B', 'E'],
// ]


// Reverse order sample
// `[1,0]` = index rotation priority for arrayList
console.log( Array.from( cartesianProduct( arrayList , [1,0] ) ) );
// ->
// [
// 	['A', 'C'],
// 	['A', 'D'],
// 	['A', 'E'],
// 	['B', 'C'],
// 	['B', 'D'],
// 	['B', 'E'],
// ]