javascript - 获取所有可能的数组组合
问题描述
我正在尝试创建一个输出数组,该数组由单个数组组成,这些数组包含使用递归的输入数组的每个组合。我在尝试弄清楚如何分离数组的元素时遇到问题。我想我需要以某种方式遍历数组并跟踪每个元素。
function getAllCombos(arr) {
const output = [];
function recursive(arr, combos = []) {
if (arr.length === 0) return output.push(combos);
combos.push(arr);
recursive(arr.slice(1), [...combos])
}
recursive(arr);
return output
}
// To check if you've completed the challenge, uncomment this code!
console.log(getAllCombos(['a', 'b'])); // -> [['a','b'], ['a'], ['b'], []]
解决方案
推荐阅读
- reactjs - 无法加载配置“react-app”以从
- python - 酸洗/保存/joblib 将复杂类转储到单个文件
- r - 将第 1 列重塑为宽,但通过 R 中第 2 列的信息
- python - 如何仅使用带有一条回归线的 lmplot 为散点使用不同的颜色?
- android - 无法将 Spring Boot 后端链接到由电容器制作的移动应用程序
- c# - 切换到 .NET Core 3.1 后,通过 iText7 读取页面的 PDF 文本不再返回内容
- javascript - 如何从数组中获取单词中的哪个字符恰好出现两次
- django - 能够让用户动态添加/编辑/删除表单字段吗?
- python - 重新排序数据框中的行以匹配另一个数据框中的行顺序
- spring-boot - sec:authentication 在 Spring Boot Security 中不起作用