hallojoe / cartesian-product

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cartesian-product

This function generates the Cartesian product of an array of arrays. Given an array of arrays, it returns a new array containing all possible combinations of elements from each array.

Use

const inputArrays = [[1, 2], ['a', 'b'], ['x', 'y']];
const result = cartesianProduct(inputArrays);
console.log(result);

/* Output: [
    [1, 'a', 'x'], 
    [1, 'a', 'y'], 
    [1, 'b', 'x'], 
    [1, 'b', 'y'], 
    [2, 'a', 'x'], 
    [2, 'a', 'y'], 
    [2, 'b', 'x'], 
    [2, 'b', 'y']
  ] */

Also see See tests.

Install

npm install @casko/cartesian-product

About

License:MIT License


Languages

Language:TypeScript 58.2%Language:JavaScript 41.8%