如何在 JavaScript 中获取所有数组的各种组合?
您可以使用您自己的函数来获取所有组合。
示例
以下是代码 −
function combination(values) { function * combinationRepeat(size, v) { if (size) for (var chr of values) yield * combinationRepeat(size - 1, v + chr); else yield v; } return [...combinationRepeat(values.length, "")]; } var output = combination([4,5]); console.log(output);
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,我的文件名是 demo306.js。
输出
这将生成以下输出 −
PS C:\Users\Amit\javascript-code> node demo306.js [ '44', '45', '54', '55' ]
广告