猿问

有大佬遇到过这个问题吗:今天在做商品规格属性时需要写一个排列组合算法,请大佬帮忙看看十分感谢

排列组合
letformats=[
[
{id:1,name:'红色'},
{id:2,name:'白色'}
],
[
{id:1,name:'100*200'},
{id:2,name:'50*200'}
],
[
{id:1,name:'20厘米'},
{id:2,name:'10厘米'}
]
]
输出结果:
letresult=[
[
{id:1,name:'红色'},
{id:1,name:'100*200'},
{id:1,name:'20厘米'}
],
[
{id:1,name:'红色'},
{id:2,name:'50*200'
},
{id:1,name:'20厘米'}
],
[
{id:1,name:'红色'},
{id:2,name:'50*200'},
{id:2,name:'10厘米'}
]
]
简化说明:
[[1,2][3,4][5,6]]
排列组合算法得到=》
[
[1,3,5],
[1,3,6],
[1,4,5],
[1,4,6],
[2,3,5],
[2,3,6],
[2,4,5],
[2,4,6]
]
眼眸繁星
浏览 258回答 2
2回答

精慕HU

constdealTwo=(arr1,arr2)=>{letresult=[];arr1.forEach(val1=>{arr2.forEach(val2=>{if(typeofval1==='number'){result.push([val1,val2]);}else{result.push(val1.concat(val2));}})})returnresult;}constpermutation=(arr)=>{constlen=arr.length;if(len
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答