Javascript 如何从一组数组中获取所有组合
例如,我有以下阵列:Javascript 如何从一组数组中获取所有组合,javascript,jquery,Javascript,Jquery,例如,我有以下阵列: a1 = ["1", "2", "3"]; a2 = ["a", "b"]; a3 = ["q", "w", "e"]; result = ["1aq", "1aw", "1ae", "1bq", "1bw", ... "3be"]; 如果没有嵌套循环(例如,也使用jquery),如何获得这个结果? 谢谢var outputArray=[]; 对于(变量i=0,最终长度=a1.length*a2.length*a3.length;i
a1 = ["1", "2", "3"];
a2 = ["a", "b"];
a3 = ["q", "w", "e"];
result = ["1aq", "1aw", "1ae", "1bq", "1bw", ... "3be"];
如果没有嵌套循环(例如,也使用jquery),如何获得这个结果?
谢谢var outputArray=[];
对于(变量i=0,最终长度=a1.length*a2.length*a3.length;i<最终长度;i++){
输出阵列[i]=a1[i%a1.length]。toString()+a2[i%a2.length]。toString()+a3[i%a3.length]。toString();
}
但这真的只是一个噱头。为什么要避免循环?我可以猜到:您事先不知道将拥有多少阵列。但这仍然是一个挑战。var outputArray=[];
对于(变量i=0,最终长度=a1.length*a2.length*a3.length;i<最终长度;i++){
输出阵列[i]=a1[i%a1.length]。toString()+a2[i%a2.length]。toString()+a3[i%a3.length]。toString();
}
但这真的只是一个噱头。为什么要避免循环?我可以猜到:您事先不知道将拥有多少阵列。但这仍然是一个挑战。我不知道嵌套循环有什么问题,但这里有一个通用的解决方案:
var a = [a1, a2, a3];
var result = [""]; // start with the empty string,
for (var i=0; i<a.length; i++) { // and repeatedly
var ai = a[i],
l = ai.length;
result = $.map(result, function(r) { // make result a new array of
var ns = []; // new combinations of
for (var j=0; j<l; j++) // each of the letters in ai
ns[j] = r + ai[j]; // and the old results
return ns;
}); // using the odds of jQuery.map with returned arrays
}
return result;
function combine() {
var target = arguments[0];
if (arguments.length === 1) {
return target; // end of chain, just return the array
}
var result = [];
// compute all combinations without the first array
var combinations = combine.apply(null, Array.prototype.slice.call(arguments, 1));
// put things together
for (var i = 0, l = target.length; i < l; i++) {
var element = target[i];
for (var j = 0, lj = combinations.length; j < lj; j++) {
result.push(element + combinations[j]);
}
}
return result;
}
// Usage
var result = combine(a1, a2, a3);
var a=[a1、a2、a3];
变量结果=[“”];//从空字符串开始,
对于(var i=0;i我看不出嵌套循环有什么问题,但这里有一个通用的解决方案:
var a = [a1, a2, a3];
var result = [""]; // start with the empty string,
for (var i=0; i<a.length; i++) { // and repeatedly
var ai = a[i],
l = ai.length;
result = $.map(result, function(r) { // make result a new array of
var ns = []; // new combinations of
for (var j=0; j<l; j++) // each of the letters in ai
ns[j] = r + ai[j]; // and the old results
return ns;
}); // using the odds of jQuery.map with returned arrays
}
return result;
function combine() {
var target = arguments[0];
if (arguments.length === 1) {
return target; // end of chain, just return the array
}
var result = [];
// compute all combinations without the first array
var combinations = combine.apply(null, Array.prototype.slice.call(arguments, 1));
// put things together
for (var i = 0, l = target.length; i < l; i++) {
var element = target[i];
for (var j = 0, lj = combinations.length; j < lj; j++) {
result.push(element + combinations[j]);
}
}
return result;
}
// Usage
var result = combine(a1, a2, a3);
var a=[a1、a2、a3];
var result=[“”];//以空字符串开头,
对于(var i=0;i一个通用递归解决方案:
var a = [a1, a2, a3];
var result = [""]; // start with the empty string,
for (var i=0; i<a.length; i++) { // and repeatedly
var ai = a[i],
l = ai.length;
result = $.map(result, function(r) { // make result a new array of
var ns = []; // new combinations of
for (var j=0; j<l; j++) // each of the letters in ai
ns[j] = r + ai[j]; // and the old results
return ns;
}); // using the odds of jQuery.map with returned arrays
}
return result;
function combine() {
var target = arguments[0];
if (arguments.length === 1) {
return target; // end of chain, just return the array
}
var result = [];
// compute all combinations without the first array
var combinations = combine.apply(null, Array.prototype.slice.call(arguments, 1));
// put things together
for (var i = 0, l = target.length; i < l; i++) {
var element = target[i];
for (var j = 0, lj = combinations.length; j < lj; j++) {
result.push(element + combinations[j]);
}
}
return result;
}
// Usage
var result = combine(a1, a2, a3);
函数组合(){
变量目标=参数[0];
if(arguments.length==1){
return target;//链的末尾,只返回数组
}
var结果=[];
//计算没有第一个数组的所有组合
var combinations=combine.apply(null,Array.prototype.slice.call(arguments,1));
//拼凑
对于(变量i=0,l=target.length;i
通用递归解决方案:
var a = [a1, a2, a3];
var result = [""]; // start with the empty string,
for (var i=0; i<a.length; i++) { // and repeatedly
var ai = a[i],
l = ai.length;
result = $.map(result, function(r) { // make result a new array of
var ns = []; // new combinations of
for (var j=0; j<l; j++) // each of the letters in ai
ns[j] = r + ai[j]; // and the old results
return ns;
}); // using the odds of jQuery.map with returned arrays
}
return result;
function combine() {
var target = arguments[0];
if (arguments.length === 1) {
return target; // end of chain, just return the array
}
var result = [];
// compute all combinations without the first array
var combinations = combine.apply(null, Array.prototype.slice.call(arguments, 1));
// put things together
for (var i = 0, l = target.length; i < l; i++) {
var element = target[i];
for (var j = 0, lj = combinations.length; j < lj; j++) {
result.push(element + combinations[j]);
}
}
return result;
}
// Usage
var result = combine(a1, a2, a3);
函数组合(){
变量目标=参数[0];
if(arguments.length==1){
return target;//链的末尾,只返回数组
}
var结果=[];
//计算没有第一个数组的所有组合
var combinations=combine.apply(null,Array.prototype.slice.call(arguments,1));
//拼凑
对于(变量i=0,l=target.length;i
另一种通用解决方案
var reduce = function(a, b) {
var r = [];
$.each(a, function(i, ai) {
$.each(b, function(j, bj) {
r.push(ai + bj);
});
});
return r;
};
另一个通用解决方案
var reduce = function(a, b) {
var r = [];
$.each(a, function(i, ai) {
$.each(b, function(j, bj) {
r.push(ai + bj);
});
});
return r;
};
没有嵌套循环。可以根据需要处理任意多个数组
var result = combine(a1, a2, a3);
function combine() {
return processArrays([].slice.call(arguments), "", []);
function processArrays(arrays, str, res) {
for (var i = 0; i < arrays[0].length; i++) {
if (arrays.length > 1) {
processArrays(arrays.slice(1), str + arrays[0][i], res);
} else {
res.push(str + arrays[0][i]);
}
}
return res;
}
}
没有嵌套循环。可以根据需要处理任意多个数组
var result = combine(a1, a2, a3);
function combine() {
return processArrays([].slice.call(arguments), "", []);
function processArrays(arrays, str, res) {
for (var i = 0; i < arrays[0].length; i++) {
if (arrays.length > 1) {
processArrays(arrays.slice(1), str + arrays[0][i], res);
} else {
res.push(str + arrays[0][i]);
}
}
return res;
}
}
如果没有某种形式的迭代,您真的无法做到这一点。jQuery中唯一相关的可能是$。each()
,但我不知道这有多大帮助。我通常避免问为什么。但为什么不使用嵌套循环呢?你在某个地方需要一个嵌套循环。即使你使用更高级别的抽象,在内部它也需要一个嵌套循环。如果你的理由是关于性能,那么简单的for
循环几乎总是fJavaScript中的astest。函数需要更多的开销。我下面的答案使用尾部递归方法来消除部分或所有循环,因为这是问题的要求,但会有性能损失。如果没有某种形式的迭代,您真的无法做到这一点。jQuery中唯一相关的可能是$。each()
,但我不知道这有多大帮助。我通常避免问为什么。但为什么不使用嵌套循环呢?你在某个地方需要一个嵌套循环。即使你使用更高级别的抽象,在内部它也需要一个嵌套循环。如果你的理由是关于性能,那么简单的for
循环几乎总是fJavaScript中的astest。函数需要更多的开销。我下面的回答使用了尾部递归方法来消除部分或全部循环,因为这是问题的要求,但会有性能损失。显然,这会重复1aq
三次。嗯,是的。完全站不住脚。向所有人道歉。显然,这重复了ts1aq
三次。嗯,是的。完全是跛脚的。向所有人道歉。你应该让函数接受一个数组数组,而不是重复调用它以避免递归…另外,嵌套的$。每个甚至比缓慢的非嵌套$还要慢。每个@Bergi,我发现这种方式在可读性和音译方面更好这对于本机JavaScript的性能提升来说是微不足道的。您应该让函数接受数组数组,而不是重复调用它以避免递归…另外,嵌套的$。每个甚至比缓慢的未列出的$慢。每个@Bergi,我发现这样更易于可读性,并将其转换为本机Java性能提升的脚本是trivial@andreaconsole:由于函数调用通常有一些开销,那么调用越少越好,这意味着前两个调用的速度越快。但是性能测试将是真正找到答案的唯一方法。@andreaconsole:由于函数调用通常有一些开销,那么fewer认为更好,所以这意味着前两个会更快。但性能测试将是真正找到答案的唯一途径。