这是一项棘手的任务,到目前为止找不到任何合适的解决方案。
让我们说我们有一系列属性:
const input = [
['attr1_a', 'attr1_b'],
['attr2_a', 'attr2_b'],
['attr3_a', 'attr3_b', 'attr3_c'],
];
所以我正在寻找一些函数getAllVariants(input);
,它将把这个数组作为一个属性并输出以下结果:
const result = [
['attr1_a', 'attr2_a', 'attr3_a'],
['attr1_a', 'attr2_a', 'attr3_b'],
['attr1_a', 'attr2_a', 'attr3_c'],
['attr1_a', 'attr2_b', 'attr3_a'],
['attr1_a', 'attr2_b', 'attr3_b'],
['attr1_a', 'attr2_b', 'attr3_c'],
['attr1_b', 'attr2_a', 'attr3_a'],
['attr1_b', 'attr2_a', 'attr3_b'],
['attr1_b', 'attr2_a', 'attr3_c'],
['attr1_b', 'attr2_b', 'attr3_a'],
['attr1_b', 'attr2_b', 'attr3_b'],
['attr1_b', 'attr2_b', 'attr3_c'],
];
感谢您的帮助!
答案 0 :(得分:-1)
const input = [{
attributes: ['attr1_a', 'attr1_b']
},
{
attributes: ['attr2_a', 'attr2_b']
},
{
attributes: ['attr3_a', 'attr3_b', 'attr3_c']
}
];
var parts = input.map(val => val.attributes),
result = parts.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
console.log(result);