如何计算不同数组之间的所有组合?
How to calculate all the combinations between different arrays?
假设我有这 3 个数组:
Shirts [White, Navy, Light Blue, Gray],
Pants [Black, Navy, Gray],
Ties [Houndstooth, Polka Dot, Herringbone, Solid]
我应该怎么做才能得到这个结果
White Shirt with Black Pants and a Houndstooth Tie,
White Shirt with Black Pants and a Polka Dot Tie,
White Shirt with Black Pants and a Herringbone Tie,
White Shirt with Black Pants and a Solid Tie,
White Shirt with Navy Pants and a Houndstooth Tie,
White Shirt with Navy Pants and a Polka Dot Tie,
White Shirt with Navy Pants and a Herringbone Tie,
White Shirt with Navy Pants and a Solid Tie,
And so on,
And so on...
只是为了计算?很简单。
将它们相乘。 5*3*4=60
个变体。
如果您需要将所有变体作为文本:
var shirts = ['White', 'Navy', 'Light Blue', 'Gray'];
var pants = ['Black', 'Navy', 'Grey'];
var ties = ['Houndstooth', 'Polka Dot', 'Herringbone', 'Solid'];
for(var s in shirts) {
for(var p in pants) {
for(var t in ties) {
document.write(shirts[s] + ' Shirt with ' + pants[p] + ' Pants and a ' + ties[t] + ' Tie<br>');
}
}
}
简单地循环其中一个,输出每个数组的索引。为此,您需要在循环内使用循环,为每个循环使用不同的索引:
var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];
for (var i = 0; i < shirts.length; i++) {
var start = shirts[i] + " shirt with ";
for (var j = 0; j < pants.length; j++) {
var middle = pants[j] + " pants and a ";
for (var k= 0; k < ties.length; k++) {
var end = ties[k] + " tie.<br />";
document.write(start + middle + end);
}
}
}
只需将三个数组的.length
相乘即可得到总和,将总和赋给一个变量,然后输出该变量:
var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];
var total_combinations = shirts.length * pants.length * ties.length;
document.write("Total number of combinations: " + total_combinations);
要获得随机输出,您可以在数组索引上使用Math.floor()
in conjunction with Math.random()
,如下所示:
var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];
var random_shirt = shirts[Math.floor(Math.random()*shirts.length)];
var random_pants = pants[Math.floor(Math.random()*pants.length)];
var random_tie = ties[Math.floor(Math.random()*ties.length)];
document.write(random_shirt + " shirt with " + random_pants + " pants and a " + random_tie + " tie.");
希望对您有所帮助! :)
假设我有这 3 个数组:
Shirts [White, Navy, Light Blue, Gray],
Pants [Black, Navy, Gray],
Ties [Houndstooth, Polka Dot, Herringbone, Solid]
我应该怎么做才能得到这个结果
White Shirt with Black Pants and a Houndstooth Tie,
White Shirt with Black Pants and a Polka Dot Tie,
White Shirt with Black Pants and a Herringbone Tie,
White Shirt with Black Pants and a Solid Tie,
White Shirt with Navy Pants and a Houndstooth Tie,
White Shirt with Navy Pants and a Polka Dot Tie,
White Shirt with Navy Pants and a Herringbone Tie,
White Shirt with Navy Pants and a Solid Tie,
And so on,
And so on...
只是为了计算?很简单。
将它们相乘。 5*3*4=60
个变体。
如果您需要将所有变体作为文本:
var shirts = ['White', 'Navy', 'Light Blue', 'Gray'];
var pants = ['Black', 'Navy', 'Grey'];
var ties = ['Houndstooth', 'Polka Dot', 'Herringbone', 'Solid'];
for(var s in shirts) {
for(var p in pants) {
for(var t in ties) {
document.write(shirts[s] + ' Shirt with ' + pants[p] + ' Pants and a ' + ties[t] + ' Tie<br>');
}
}
}
简单地循环其中一个,输出每个数组的索引。为此,您需要在循环内使用循环,为每个循环使用不同的索引:
var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];
for (var i = 0; i < shirts.length; i++) {
var start = shirts[i] + " shirt with ";
for (var j = 0; j < pants.length; j++) {
var middle = pants[j] + " pants and a ";
for (var k= 0; k < ties.length; k++) {
var end = ties[k] + " tie.<br />";
document.write(start + middle + end);
}
}
}
只需将三个数组的.length
相乘即可得到总和,将总和赋给一个变量,然后输出该变量:
var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];
var total_combinations = shirts.length * pants.length * ties.length;
document.write("Total number of combinations: " + total_combinations);
要获得随机输出,您可以在数组索引上使用Math.floor()
in conjunction with Math.random()
,如下所示:
var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];
var random_shirt = shirts[Math.floor(Math.random()*shirts.length)];
var random_pants = pants[Math.floor(Math.random()*pants.length)];
var random_tie = ties[Math.floor(Math.random()*ties.length)];
document.write(random_shirt + " shirt with " + random_pants + " pants and a " + random_tie + " tie.");
希望对您有所帮助! :)