我有例如这些数组:
a1 = ["1", "2", "3"];
a2 = ["a", "b"];
a3 = ["q", "w", "e"];
result = ["1aq", "1aw", "1ae", "1bq", "1bw", ... "3be"];
没有嵌套循环(例如,也使用 jquery)如何获得这个?谢谢
我有例如这些数组:
a1 = ["1", "2", "3"];
a2 = ["a", "b"];
a3 = ["q", "w", "e"];
result = ["1aq", "1aw", "1ae", "1bq", "1bw", ... "3be"];
没有嵌套循环(例如,也使用 jquery)如何获得这个?谢谢
我看不出嵌套循环有什么问题,但这是一个通用的解决方案:
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;
没有嵌套循环。可以根据需要处理任意数量的数组。
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;
}
}
或对该功能略有不同的看法:
function combine() {
return processArrays([].slice.call(arguments), "", []);
function processArrays(arrays, str, res) {
if (arrays.length === 0)
res.push(str)
else
for (var i = 0; i < arrays[0].length; i++)
processArrays(arrays.slice(1), str + arrays[0][i], res);
return res;
}
}
这是一个无循环版本:
var result = combine(a1, a2, a3);
function combine() {
return processArrays(arguments[0], [].slice.call(arguments, 1), "", []);
function processArrays(head, tail, str, res) {
if (head === undefined)
res.push(str)
else
processArray(head[0], head.slice(1), tail, str, res);
return res;
}
function processArray(head, tail, arrays, str, res) {
if (head) {
processArrays(arrays[0], arrays.slice(1), str + head, res);
processArray(tail[0], tail.slice(1), arrays, str, res)
}
}
}
通用递归解决方案:
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 reduce = function(a, b) {
var r = [];
$.each(a, function(i, ai) {
$.each(b, function(j, bj) {
r.push(ai + bj);
});
});
return r;
};
var result = reduce(reduce(a1, a2), a3);
var outputArray = [];
for(var i = 0, finalLength = a1.length * a2.length * a3.length; i < finalLength; i++) {
outputArray[i] = a1[i % a1.length].toString() + a2[i % a2.length].toString() + a3[i % a3.length].toString();
}
但这真的只是一个噱头。为什么要避免循环?我可以猜到:您事先不知道您将拥有多少个阵列。但这仍然是一个挑战。