简体   繁体   English

选择随机数组,然后从该数组中选择一个元素

[英]choose random array and then an element from this array

I'm trying to figure out if it's possible to first choose a random array (on load) and then an element from this chosen array. 我试图弄清楚是否有可能首先选择一个随机数组(在加载时),然后从这个选择的数组中选择一个元素。 So for example I have: 例如,我有:

    var colorsOne = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsTwo = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsThree = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsFour = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsFive = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsSix = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsSeven = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsEight = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsNine = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsTen = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsEleven = ["#CCCCCC","#333333","#990099","#990099"];  
    var colorsTwelve = ["#CCCCCC","#333333","#990099","#990099"];  

12 arrays (each one will eventually be populated with different hex codes), I first need to choose one of these array and then one hex code (at random) from this array. 12个数组(每个数组最终将填充不同的十六进制代码),我首先需要从这些数组中选择一个,然后从该数组中选择一个十六进制代码(随机)。

eg 例如

var rand = Math.floor(Math.random() * 4);               
$('.header-wrap').css("background-color", colorsEight[rand]);

this will take a random element from coloursEight array, but I've had to program that, I need to choose an array randomly first and THEN choose an element from this array. 这将从coloursEight数组中获取一个随机元素,但是我不得不对此进行编程,我需要首先随机选择一个数组,然后从该数组中选择一个元素。

Any suggestions for this would be greatly appreciated! 任何建议,将不胜感激!

The answer is pretty much inherent in the question. 答案几乎是问题固有的。 :-) Put the arrays into an array, then do the same thing you're doing: :-)将数组放入数组,然后做同样的事情:

var colors = [
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"],
    ["#CCCCCC","#333333","#990099","#990099"]
];

var array = colors[Math.floor(Math.random() * colors.length)];
var color = array[Math.floor(Math.random() * array.length)];
$('.header-wrap').css("background-color", color);

If for some reason you need those separate array variables, you can still gather them together: 如果由于某些原因需要这些单独的数组变量,则仍可以将它们收集在一起:

var colorsOne = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsTwo = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsThree = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsFour = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsFive = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsSix = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsSeven = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsEight = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsNine = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsTen = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsEleven = ["#CCCCCC","#333333","#990099","#990099"];  
var colorsTwelve = ["#CCCCCC","#333333","#990099","#990099"]
var colors = [
    colorsOne,
    colorsTwo,
    colorsThree,
    colorsFour,
    colorsFive,
    colorsSix,
    colorsSeven,
    colorsEight,
    colorsNine,
    colorsTen,
    colorsEleven,
    colorsTwelve
];

var array = colors[Math.floor(Math.random() * colors.length)];
var color = array[Math.floor(Math.random() * array.length)];
$('.header-wrap').css("background-color", color);

You simply need to generate a 2-dimensional array from all your existing arrays. 您只需要从所有现有数组生成二维数组。

var super_array = [colorsOne, colorsTwo, ...];

Then simply grab a random index for super_array and you now have a single array that you can use to your hearts desire. 然后,只需获取super_array的随机索引,您便有了一个可以满足您内心需求的数组。

var rand_x = Math.floor(Math.random() * 4); 
var rand_y = Math.floor(Math.random() * 4);              
$('.header-wrap').css("background-color", super_array[rand_x][rand_y]);
function randomArrVal(arr) {
 return arr[Math.floor(arr.length * Math.random())];
}
var array2D = [
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"],
  ["#CCCCCC","#333333","#990099","#990099"]
];

$('.header-wrap').css("background-color", randomArrVal(randomArrVal(array2D)));

There seems to be no reason for creating named arrays. 似乎没有理由创建命名数组。 You could just create a Multi-Dimensional Array and get the Color you want off it. 您可以只创建一个多维数组,然后从中获得所需的颜色。 The snippet below demonstrates that.... 下面的代码片段演示了这一点。

    // BUNDLE ALL THE INDIVIDUAL ARRAY OF COLORS
    // INTO ONE SINGLE ARRAY THUS CREATING A MULTI-DIMENSIONAL ARRAY.
    var arrHexColors    = [
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"],
        ["#CCCCCC","#333333","#990099","#990099"]
    ];

    // IF NECESSARY (SHOULD THE KEYS BE IMPORTANT TO YOU);
    // CREATE AN ARRAY TO HOLD THE KEYS TO THE INDIVIDUAL ARRAY...
    // THE IDEA HERE IS TO CREATE A PARALLEL ARRAY, WITH THE KEYS HERE
    // CORRESPONDING TO THE KEYS REPRESENTING ITS CONTENT IN THE MULTI-DIMENSIONAL ARRAY.
    var arrColorKeys    = [
        'colorsOne',
        'colorsTwo',
        'colorsThree',
        'colorsFour',
        'colorsFive',
        'colorsSix',
        'colorsSeven',
        'colorsEight',
        'colorsNine',
        'colorsTen',
        'colorsEleven',
        'colorsTwelve'
    ];

    // GENERATE A RANDOM NUMBER THAT SHOULD BE 
    // "ONE" LESS THAN THE LENGTH OF THE MULTI-DIMENSIONAL ARRAY
    var arrRandomColor  = arrHexColors[ Math.floor(Math.random() * arrHexColors.length) ];

    // DO THE SAME FOR THE COLOR KEYS ARRAY - IF NEEDED....
    var arrRandomKey    = arrRandomColor[ Math.floor(Math.random() * arrRandomColor.length) ];

    // USING THE GENERATED RANDOM NUMBER PICK THE ARRAY 
    // CORRESPONDING TO THAT KEY
    var randomColor     = arrRandomColor[ Math.floor(Math.random() * arrRandomColor.length) ];

    $('.header-wrap').css("background-color", randomColor);

    console.log(randomColor);

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

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