const input = [1, 2, 3]; const output = [[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]; function composite(init, arr) { if(arr.length === 0) { return []; } let tmp = []; for(let i=0;i<arr.length;i++) { tmp.push([...init, arr[i]]); if(arr.length > 1&& i<arr.length-1) { tmp = [...tmp, ...composite([...init, arr[i]], arr.slice(i+1))]; // tmp.push(composite([...init, arr[i]], arr.slice(i+1))); } } return tmp; } const result = composite([], [1,2,3,4]); console.log(result.slice());