Soccerrlife
Soccerrlife

Reputation: 79

Create all combinations of an Object-array in JavaScript

I would like to find all combinations of the options-property of an n-array. In the example the array has a length of 3, but the function should also work with a be bigger or smaller array size.

var arr = [{
    name: 'Fruit',
    options: ['apple', 'kiwi']
}, {
    name: 'Food',
    options: ['bread', 'rice']
}, {
    name: 'Drink',
    options: ['water', 'cola']
}]

The result should result print the following statements

Fruit: apple | Food: bread | Drink: water
Fruit: apple | Food: bread | Drink: cola
Fruit: apple | Food: rice | Drink: water
Fruit: apple | Food: rice | Drink: cola
Fruit: kiwi | Food: bread | Drink: water
Fruit: kiwi | Food: bread | Drink: cola
Fruit: kiwi | Food: rice | Drink: water
Fruit: kiwi | Food: rice | Drink: cola

I have read this answer Finding All Combinations (Cartesian product) of JavaScript array values, but in my example the array is an object and I cant figure out how to get the properties. This is what I have so far:

function allPossibleCases(arr) {
    if (arr.length === 0) {
        return [];
    } else if (arr.length ===1){
        return arr[0].options;
    } else {
        var result = [];
        var allCasesOfRest = allPossibleCases(arr.slice(1));  // recur with the rest of array
        for (var c in allCasesOfRest) {
            for (var i = 0; i < arr[0].options.length; i++) {
                console.log(arr[0].name, ": ", arr[0].options[i], "| ", allCasesOfRest[c])
            }
        }
        return result;
    }
}

The output is:

Food :  bread |  water
Food :  rice |  water
Food :  bread |  cola
Food :  rice |  cola

What am I missing?

Upvotes: 1

Views: 172

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386868

You could get the values, build a cartesian product and map the properties.

var array = [{ name: 'Fruit', options: ['apple', 'kiwi'] }, { name: 'Food', options: ['bread', 'rice'] }, { name: 'Drink', options: ['water', 'cola'] }],
    keys = array.map(({ name }) => name),
    result = array
        .map(({ options }) => options)
        .reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []))
        .map(a => Object.assign(...keys.map((k, i) => ({ [k]: a[i] }))));
	
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 6

Related Questions