array all combinations

by Chan Wu

Babel + JSX

console.clear();

let r = [
	[1, 2],
    [2, 3],
    [5],
    [9],
    [3, 4],
];

function combinations(arrays, i = 0) {
	if (arrays[i] === undefined) {
    	return [];
    }
    
    if (i == arrays.length - 1) {
    	return arrays[i];
    }
    
    let tmp = combinations(arrays, i + 1);
    let results = [];
    
	for (let v of arrays[i]) {
    	for (let t of tmp) {
			let result = $.isArray(t) ? [v].concat(t) : [v, t];
            results.push(result);
        }
    }

    return results;
}

console.log(combinations(r));