【问题标题】:How to calculate all the combinations between different arrays?如何计算不同数组之间的所有组合?
【发布时间】:2017-08-15 23:28:09
【问题描述】:

假设我有这 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...

【问题讨论】:

  • 我对JS有点生疏,所以请解释一下你是做什么的。
  • 你有这方面的代码吗?如果没有,请先写一些并尝试一下,然后我们会看到我们可以做些什么来提供帮助。
  • 您的问题到底是什么?你试过什么?您发布的数组有 75 种可能的组合。
  • 最基本的解决方案可能是使用 3 个嵌套的 for 循环来遍历所有内容

标签: javascript html combinations permutation calculator


【解决方案1】:

只需循环其中一个,输出每个数组的索引。为此,您需要在循环内使用循环,为每个循环使用不同的索引:

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()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.");

希望这会有所帮助! :)

【讨论】:

  • 如何在 html 中打印?
  • 您可以使用document.write()。我刚刚更新了我的答案以涵盖这一点:)
  • 现在完全不同的问题。单击按钮时如何输出其中一个随机组合?
  • 你太棒了。请原谅我对javascript的无知。我几乎没有使用过它。最后一个问题,如果你愿意的话,我怎样才能用衣服的颜色/纹理打印一个正方形?
  • 这实际上做起来相当复杂,并且将该逻辑与最初的问题结合起来会使其过于宽泛,并使 StackOverflow 的问题为“off-topic”。请将您尝试执行的每个操作分解为特定操作,并将每个操作作为新的单独问题提出。如果有助于解释您的观点,您可以使用this link 链接到此问题:)
【解决方案2】:

只是为了计算?这很容易。

将它们相乘。 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>');
        }
      } 
    }

【讨论】:

  • 我想用 html 打印,但是document.write 不起作用。我能做什么?
  • 这超出了这个问题的范围。阅读手册,伙计。试试&lt;div id=out&gt;&lt;/div&gt;document.getElementById('out').innerHTML+="string"
  • 仅供参考,它被认为是 bad practice to use for/in 用于遍历数组。
  • 感谢您的信息。但是在这种简单的情况下使用它是可以的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
相关资源
最近更新 更多