【问题标题】:Accessing a random variable from an array for use in another varaible访问数组中的随机变量以用于另一个变量
【发布时间】:2020-12-21 12:09:49
【问题描述】:

我有这个特定变量的四种可能性之一 -->

var lowVelocity = Math.random() * (45 - 30) + 30;
var medVelocity = Math.random() * (60 - 45) + 45;
var highVelocity = Math.random() * (80 - 60) + 45;
var hwyVelcoity = Math.random() * (100 - 80) +80;

在一种情况下,我可能有 lowVelocity 和 medVelocity,所以我只想在这两者之间随机选择。所以我这样做了:

const velocities = ["lowVelocity", "medVelocity"];
const randomUrban = Math.floor(Math.random() * velocities.length);

这很有效 - 它现在选择我感兴趣的两种速度之一。低速在 30 到 45 之间,中速在 45 到 60 之间。 在我的 innerHTML 中,我想打印从数组中选择的变量返回的数字。当用户单击 this 时,将启动一个包含 this 的函数。

document.getElementById("scenario").innerHTML = randomUrban; 

但是当它在 HTML 中打印时,它不会打印与变量关联的数字,而是打印它选择的数组编号(如 0 或 1)。

如何获取要打印的变量(例如,如果它选择了 lowVelocity,那么它打印的数字将是找到的随机数 lowVelocity,例如 39)而不是数组编号?

【问题讨论】:

    标签: javascript html arrays random


    【解决方案1】:

    在你的情况下,只需使用

    document.getElementById("scenario").innerHTML = window[velocities[randomUrban]]; 
    

    var lowVelocity = Math.random() * (45 - 30) + 30;
    var medVelocity = Math.random() * (60 - 45) + 45;
    var highVelocity = Math.random() * (80 - 60) + 45;
    var hwyVelcoity = Math.random() * (100 - 80) + 80;
    const velocities = ["lowVelocity", "medVelocity"];
    const randomUrban = Math.floor(Math.random() * velocities.length);
    console.log(randomUrban);// random from velocities
    console.log(velocities[randomUrban]); //get variable name
    console.log(window[velocities[randomUrban]]); // search in scope where the variables are declared in this case window

    【讨论】:

    • 谢谢!这按预期工作。感谢您的帮助:) 正在绘制空白! document.getElementById("scenario").innerHTML = window[velocities[randomUrban]]; };
    • 嗨@PaulDavis,如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
    【解决方案2】:

    document.getElementById("scenario").innerHTML = velocities[randomUrban]; 怎么样?

    【讨论】:

    • 啊,这会打印“randomUrban”输出(这很好),但不会打印与 randomUrban 关联的变量 :(
    【解决方案3】:

    好吧,您正在尝试打印错误的变量; randomUrban 是包含所选velocities 数组索引的变量。

    一个快速(但相当肮脏)的解决方法是打印eval(velocities[randomUrban]),但eval() 由于安全问题已被弃用

    更长但更好的解决方案是将您的速度分组到一个对象类型的变量中并访问其键以检索数值:

    const velObj = {
        lowVelocity: Math.random() * (45 - 30) + 30,
        medVelocity: Math.random() * (60 - 45) + 45,
        highVelocity: Math.random() * (80 - 60) + 45,
        hwyVelocity: Math.random() * (100 - 80) + 80
    };
    
    const velocities = ["lowVelocity", "medVelocity"];
    const randomUrban = Math.floor(Math.random() * velocities.length);
    const velKey = velocities[randomUrban];
    
    document.getElementById("scenario").innerHTML = velObj[velKey];
    <div id="scenario"></div>

    【讨论】:

      【解决方案4】:

      您可以只使用一个数组为您完成所有这些工作!

      如果将速度添加到数组中,则随机数可以用作该数组的索引。在下面的代码中,我们使用这个数组的长度来设置随机数的限制,那么你可以简单地从velocities数组中随机数所指示的位置获取值:

      velocities[randomUrban][1]
      

      这样得到randomUrban所指位置的子数组,[1]得到行中的第二个元素,即计算出的速度。

      /* Add the velocities to an array or easier management */
      var velocities= [
          ["lowVelocity", Math.random() * (45 - 30) + 30],
          ["medVelocity", Math.random() * (60 - 45) + 45],
          ["highVelocity",Math.random() * (80 - 60) + 45],
          ["hwyVelcoity" ,Math.random() * (100 - 80) +80]
      ]
      
      /* Pick a random position from the velocities */
      const randomUrban = Math.floor(Math.random() * velocities.length);
      
      /* Print the velocity at that position. (Also includes the velocity name for illustration) */
      document.getElementById("scenario").innerHTML = 
                    velocities[randomUrban][0] + " = " + velocities[randomUrban][1];
      <div id="scenario"></div>

      注意:在您的示例中,您只使用了 4 个速度中的 2 个,因此您只需将要选择的速度添加到 randomVelocities 数组中即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-24
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多