如何从JavaScript中的数组中选择一个随机元素?

2021年3月17日15:05:06 发表评论 611 次浏览

任务是使用JavaScript从数组中选择随机元素

方法1:

  • 采用Math.random()函数获取介于(0-1, 1除外)之间的随机数。
  • 将其乘以数组长度即可得到介于(0-arrayLength)之间的数字。
  • 采用Math.floor()获取从(0到arrayLength-1)的索引。

例子:本示例实现了上述方法。

<!DOCTYPE HTML> 
< html > 
  
< head > 
     < title > 
         How to select a random element
         from array in JavaScript ?
     </ title >
</ head >
  
< body style = "text-align:center;" > 
      
     < h1 style = "color:green;" > 
         lsbin 
     </ h1 > 
      
     < p id = "GFG_UP" style = 
         "font-size: 15px; font-weight: bold;" > 
     </ p >
      
     < button id = "button" onclick = "GFG_Fun()" > 
         click here 
     </ button > 
      
     < p id = "GFG_DOWN" style = 
         "font-size: 24px; font-weight: bold; color: green;" > 
     </ p > 
      
     < script > 
         var up = document.getElementById('GFG_UP'); 
         var down = document.getElementById('GFG_DOWN'); 
          
         var arr = ["GFG_1", "lsbin", "Geeks", "Computer Science Portal"];
          
         up.innerHTML = "Click on the button to check "
                + "the type of element.< br >< br >" + arr;
          
         function GFG_Fun() {
             down.innerHTML = 
                 arr[Math.floor(Math.random() * arr.length)];
         } 
     </ script > 
</ body > 
  
</ html >

输出如下:

在单击按钮之前:

如何从JavaScript中的数组中选择一个随机元素?1

单击按钮后:

如何从JavaScript中的数组中选择一个随机元素?2

方法二:

  • 的随机(a, b)方法用于生成介于(a至b, b除外)之间的数字。
  • 取下限范围为从(1到arrayLength)的数字。
  • 减去1得到的索引范围是(0到arrayLength-1)。

例子:本示例实现了上述方法。

<!DOCTYPE HTML> 
< html > 
  
< head > 
     < title > 
         How to select a random element
         from array in JavaScript ?
     </ title >
</ head >
  
< body style = "text-align:center;" > 
      
     < h1 style = "color:green;" > 
         lsbin 
     </ h1 > 
      
     < p id = "GFG_UP" style =
         "font-size: 15px; font-weight: bold;" > 
     </ p >
      
     < button id = "button" onclick = "GFG_Fun()" > 
         click here 
     </ button > 
      
     < p id = "GFG_DOWN" style = 
         "font-size: 24px; font-weight: bold; color: green;" > 
     </ p > 
      
     < script > 
         var up = document.getElementById('GFG_UP'); 
         var down = document.getElementById('GFG_DOWN'); 
          
         var arr = ["GFG_1", "lsbin", "Geeks", "Computer Science Portal"];
          
         up.innerHTML = "Click on the button to select"
                         + " random element from the"
                         + " array.< br >< br >" + arr;
          
         function random(mn, mx) { 
             return Math.random() * (mx - mn) + mn; 
         } 
          
         function GFG_Fun() {
             down.innerHTML = arr[Math.floor(random(1, 5))-1];
         } 
     </ script > 
</ body > 
  
</ html >

输出如下:

在单击按钮之前:

如何从JavaScript中的数组中选择一个随机元素?3

单击按钮后:

如何从JavaScript中的数组中选择一个随机元素?4

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: