I'm trying to get a permutations function to work but it's confusing me.
example dataset:
{
a: [1,2],
b: [1,2,3],
c: [1,2]
}
desired output:
[
[ 'a-1', 'b-1', 'c-1' ],
[ 'a-1', 'b-1', 'c-2' ],
[ 'a-1', 'b-2', 'c-1' ],
[ 'a-1', 'b-2', 'c-2' ],
[ 'a-1', 'b-3', 'c-1' ],
[ 'a-1', 'b-3', 'c-2' ],
[ 'a-2', 'b-1', 'c-1' ],
[ 'a-2', 'b-1', 'c-2' ],
[ 'a-2', 'b-2', 'c-1' ],
[ 'a-2', 'b-2', 'c-2' ],
[ 'a-2', 'b-3', 'c-1' ],
[ 'a-2', 'b-3', 'c-2' ],
]
So far I've iterated over the Object, then iterated over the Array for each key and then need to do that all again..
To start I broke the values out like so:
[
[ 'a-1', 'a-2' ],
[ 'b-1', 'b-2', 'b-3' ],
[ 'c-1', 'c-2' ]
]
then iterated over them:
var list_joined = []
var list = []
array.forEach((key, index) => {
key.forEach((value) => {
var tmp = [value]
array.forEach((_value, _index) => {
if(_index != index) {
tmp.push(_value[0])
}
})
tmp = tmp.sort()
if(list_joined.indexOf(tmp.join('_')) < 0) {
list_joined.push(tmp.join('_'))
list.push(tmp)
}
})
})
result
[
[ 'a-1', 'b-1', 'c-1' ],
[ 'a-1', 'b-1', 'c-2' ],
[ 'a-1', 'b-2', 'c-1' ],
[ 'a-1', 'b-3', 'c-1' ],
[ 'a-2', 'b-1', 'c-1' ]
]
I can't quite put my finger on where to fix the iterative process.