Getting started with Recursion – Get all Combinations of Elements from Arrays in Javascript

function getCombinations(params, combinationsResults){
    if(params.length == 0) return combinationsResults;
    var head = params[0];
    var tail = params.slice(1);
    var combinationsResultsCurrent = [];
    if(Array.isArray(head)){
        _.uniq(head).forEach(function(item){
            if(combinationsResults.length == 0){
                combinationsResultsCurrent.push(item);
            } else {
                combinationsResults.forEach(function(previousResultItem){
                    combinationsResultsCurrent.push(previousResultItem.concat([item]));
                });
            }
        });
    } else {
        if(combinationsResults.length == 0){
            combinationsResultsCurrent.push(head);
        } else {
            combinationsResults.forEach(function(previousResultItem){
                combinationsResultsCurrent.push([previousResultItem].concat([head]));
            });
        }
    }
    return getCombinations(tail, combinationsResultsCurrent);

example:

Suppose we have a query with IN caluses:

SELECT * FROM custom_table WHERE user_id = 'user1' AND location IN ('home', 'work', AND date IN ('2017-01-10', '2017-01-11'))

AND would like to get all combinations of parameters to generate queries without IN conditions:

SELECT * FROM custom_table WHERE user_id = [value for user_id] AND location = [value for possible location] AND date = [value for possible date]

In order to get all possible combinations of parameters for equivalent queries, we can run the function above:

var params = ['user1', ['home', 'work'], ['2017-01-10', '2017-01-11']];

if you want to reproduce, please indicate the source:
Getting started with Recursion – Get all Combinations of Elements from Arrays in Javascript - CodeDay