简体   繁体   English

Javascript! 如何使用随机数组选择随机数组?

[英]Javascript! How do I choose a random array WITH a random array?

Here's what I'm trying to do.这就是我想要做的。 I just got this random concoction to work after spending 3 hours at http://www.w3schools.com/js/http://www.w3schools.com/js/ 上花了 3 个小时后,我才开始使用这种随机混合物

<!DOCTYPE html>
<html>
<body>

<p>Loopin through an array using a for loop:</p>

<p id="demo"></p>

<script>

var myShows = ['Bones', 'Psych', 'Big Bang Theory', 'Mad Men',
'Breaking Bad', 'Modern Family', 'Game of Thrones', 'Dexter'];

var show = myShows[Math.floor(Math.random() * myShows.length)];

document.getElementById("demo").innerHTML = show




</script>
<input type="button" value="Refresh Page" onClick="window.location.reload()">
</body>
</html>

How could I make a random generator to randomly pull from two or more lists?我怎样才能让一个随机生成器从两个或多个列表中随机抽取? How would I make it do it on certain reuslts?我如何让它在某些结果上做到这一点?

The outcome Im looking for: Instead of 1 random result per click,我正在寻找的结果:而不是每次点击 1 个随机结果,

I click once, and it 1. runs through and array of "shows, cartoons, theatre"我点击一次,它 1. 贯穿和“表演、卡通、戏剧”的数组

If the result is shows, THEN RANDOMLY generate one of the show results If the result is cartoons, Then Randomly generate a cartoon result If the results is Theatre, Then Randomly Generate a theatre result如果结果是shows, THEN RANDOMLY 生成一个show 结果 如果结果是cartoons, Then 随机生成一个卡通结果 如果结果是Theatre, Then Randomly Generate a Theater 结果

Shows = "bob newhard, mr.blevedere, growing pains"
cartoons = "chowder, flapjack, voltron"
Thearte = "Phantom of the opera, Lion king, Roots"

How would I do this Array, on an array?我将如何在数组上执行此数组?

Explanation解释

I would use a 2D array - this practically is an array, where each item is another array.我会使用二维数组 - 这实际上是一个数组,其中每个项目都是另一个数组。 You can have arrays with as many "dimensions" as you want, but here two is what we want.您可以根据需要拥有具有任意多个“维度”的数组,但这里有两个是我们想要的。

So, your array would be:所以,你的数组将是:

var genres = [["bob newhard","mr.blevedere","growing pains"],["chowder","flapjack","voltron"],["Phantom of the opera","Lion king","Roots"]]

Now, I would randomly select a sub-array from genres .现在,我将随机从genres选择一个子数组。 Lets do it like so:让我们这样做:

var shows = genres[Math.floor(Math.random()*genres.length)]

Now, we just need to select a random item from that array.现在,我们只需要从数组中随机选择一个项目。 This is practically the same code, except we replace genres with shows :这几乎是相同的代码,但我们替换genresshows

var show = shows[Math.floor(Math.random()*shows.length)]

Solution解决方案

(You'll need to click the button to generate a show the first time) (您需要第一次点击按钮来生成一个节目)

 function newshow() { var genres = [["bob newhard","mr.blevedere","growing pains"],["chowder","flapjack","voltron"],["Phantom of the opera","Lion king","Roots"]]; var shows = genres[Math.floor(Math.random()*genres.length)]; var show = shows[Math.floor(Math.random()*shows.length)]; document.getElementById("show").innerHTML = show; }
 <button onclick="newshow()">New Show!</button><br> <div id="show"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM