-init Data
{
sport: ['s_1', 's_2', 's_3'],
date: ['d_1', 'd_2'],
category: ['c_1']
}
['sport', 'date'] or ['sport', 'date', 'category']
If input data = ['sport', 'date']
: output have to show below.
[
's_1',
's_1|d_1',
's_1|d_2',
's_2',
's_2|d_1',
's_2|d_2',
's_3',
's_3|d_1',
's_3|d_2'
]
If input data = ['sport', 'date', 'category']
: output have to show below.
['s_1',
's_1|d_1',
's_1|d_1|c_1',
's_1|d_2',
's_1|d_2|c_1'
's_2',
's_2|d_1',
's_2|d_1|c_1',
's_2|d_2',
's_2|d_2|c_1',
's_3',
's_3|d_1',
's_3|d_1|c_1',
's_3|d_2',
's_3|d_2|c_1'
]
You can use a recursive generator function:
var data = {sport: ['s_1', 's_2', 's_3'], date: ['d_1', 'd_2'], category: ['c_1']}
function* combos(d, c = []){
if (c.length > 0){
yield c.join('|')
}
if (d.length > 0){
for (var i of data[d[0]]){
yield* combos(d.slice(1), [...c, i])
}
}
}
console.log([...combos(['sport', 'date'])])
console.log([...combos(['sport', 'date', 'category'])])