问题:给定一个不重复集合,编写代码,能够输出其所有子集合。例如对于输入集合[1,2,3],返回以下集合,顺序不限:
[
[], [ 1 ],
[ 2 ], [ 2, 1 ],
[ 3 ], [ 3, 1 ],
[ 3, 2 ], [ 3, 2, 1 ]
]
答案:
const test = arr => (
arr.reduce((prev, next) => [
...prev,
...prev.map(item => [
next,
...item
])
], [[]])
)
console.log(test([1,2,3]));