简体   繁体   English

将相同的随机数组索引值应用于两个不同的变量

[英]Applying the same randomized array index value to two different variables

So basically this is what I'm trying to do. 所以基本上这就是我想要做的。 My code runs an AJAX get request on some external JSON data that's stored in arrays (as you can see below). 我的代码对存储在数组中的某些外部JSON数据运行AJAX get请求(如下所示)。

My code works great. 我的代码很棒。

However I want to apply the same random value on the actual entry number that it's retrieving for both variable sa and ss. 但是我想将相同的随机值应用于它为变量sa和ss检索的实际条目号。

var sa = (index.Of['10']['blah:blah'].label + ' ');
var ss = (index.Of['10']['blah:blah'].label);
var sS = (sa +  ss);

So I can do this with one variable with no issue, in the following code by doing it this way. 因此,通过以下方式,我可以在下面的代码中毫无问题地使用一个变量来执行此操作。

var sa = (index.Of[Math.floor(Math.random() * 10) + 1]['blah:blah'].label + ' ');
var ss = (index.Of[Math.floor(Math.random() * 10) + 1]['blah:blah'].label);
var sS = (sa +  ss);

BUT the issue is that I don't know of a way to apply the same math.floor randomization array value to both variables. 但是问题是我不知道将相同的math.floor随机数组值应用于这两个变量的方法。

I'm stumped. 我很沮丧

You can randomize a value once, save it to a variable and reuse: 您可以将值随机化一次,将其保存到变量中并重复使用:

var rand = Math.floor(Math.random() * 10) + 1;

var sa = (index.Of[rand]['blah:blah'].label + ' ');
var ss = (index.Of[rand]['blah:blah'].label);
var sS = (sa +  ss);
var r = Math.floor(Math.random() * 10) + 1;
var sa = (index.Of[r]['blah:blah'].label + ' ');
var ss = (index.Of[r]['blah:blah'].label);
var sS = (sa +  ss);

?

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

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