排列组合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]]
2 回答
精慕HU
TA贡献1845条经验 获得超8个赞
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<=1){returnarr;}letresult=arr[0];for(leti=1;iresult=dealTwo(result,arr[i]); }returnresult;}console.log(permutation([[1,2],[3,4],[5,6]]));思路就是2个2个的进行遍历合并
添加回答
举报
0/150
提交
取消