Skip to content

Instantly share code, notes, and snippets.

@arbaz52
Created August 15, 2023 06:40
Show Gist options
  • Save arbaz52/61a879102e2a75b38677506af2730ec0 to your computer and use it in GitHub Desktop.
Save arbaz52/61a879102e2a75b38677506af2730ec0 to your computer and use it in GitHub Desktop.
const recursive = (input: string[][]): string[][] => {
if (input.length === 0) return [];
if (input.length === 1) return input[0].map((i) => [i]);
const arr = input[0];
const perm = recursive(input.slice(1));
const result = arr.map((a) => perm.map((p) => [a, ...p])).flat(1);
return result;
};
@arbaz52
Copy link
Author

arbaz52 commented Aug 15, 2023

const input = [
    [
        "Small",
        "Medium",
        "Large"
    ],
    [
        "Red",
        "Green"
    ],
    [
        "50g",
        "100g",
        "250g"
    ]
]
const output = [
    [
        "Small",
        "Red",
        "50g"
    ],
    [
        "Small",
        "Red",
        "100g"
    ],
    [
        "Small",
        "Red",
        "250g"
    ],
    [
        "Small",
        "Green",
        "50g"
    ],
    [
        "Small",
        "Green",
        "100g"
    ],
    [
        "Small",
        "Green",
        "250g"
    ],
    [
        "Medium",
        "Red",
        "50g"
    ],
    [
        "Medium",
        "Red",
        "100g"
    ],
    [
        "Medium",
        "Red",
        "250g"
    ],
    [
        "Medium",
        "Green",
        "50g"
    ],
    [
        "Medium",
        "Green",
        "100g"
    ],
    [
        "Medium",
        "Green",
        "250g"
    ],
    [
        "Large",
        "Red",
        "50g"
    ],
    [
        "Large",
        "Red",
        "100g"
    ],
    [
        "Large",
        "Red",
        "250g"
    ],
    [
        "Large",
        "Green",
        "50g"
    ],
    [
        "Large",
        "Green",
        "100g"
    ],
    [
        "Large",
        "Green",
        "250g"
    ]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment