728x90
반응형
객체정렬
-
[javascript] 조합, 순열 및 객체 정렬Development/javascript & nodejs 2022. 6. 23. 12:44
재귀 함수를 이용한 조합 //조합-combination let getCombi = (arr,selectnum)=>{ const rest = []; if (selectnum === 1){ return arr.map((v)=>[v]); } arr.forEach((fixed, index, origin)=>{ const rest = origin.slice(index + 1); const combi = getCombi(rest, selectnum - 1); const fixed_combi = combi.map((v)=>[fixed, ...v]); result.push(...fixed_combi); }); return result; } 조합은 중복을 하지 않으며 모든 경우의 수를 조합할 수 있다. 예를 들어 1,2..