2016-12-15 81 views
0

我想弄清楚是否有可能首先選擇一個隨機數組(加載),然後從這個選定的數組中的元素。因此,例如,我有:選擇隨機數組,然後從這個數組中的元素

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陣列(每一個最終將被用不同的十六進制代碼填充),我首先需要選擇從這個數組這些陣列中的一個,然後一個十六進制碼(隨機)。

例如

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

這將需要一個隨機元素從coloursEight數組,但我不得不編程,我需要隨機先選擇一個陣列,然後從這個數組中的元素。

任何建議,將不勝感激!

+0

你可以把它們組織成二維數組結構嗎? –

+0

使用對象或數組來存儲十二個數組,而不是單獨的標識符。 – guest271314

回答

2

答案在這個問題上是非常固有的。 :-)把陣列到一個數組,然後做你正在做同樣的事情:

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); 

如果由於某種原因,你需要這些獨立的數組變量,你仍然可以聚集在一起:

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); 
1

您只需從所有現有陣列生成一個2維數組。

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

然後簡單的搶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]); 
0
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))); 
0

似乎沒有理由創建命名數組。你可以創建一個多維數組並獲取你想要的顏色。下面的代碼片段證明了這一點....

// 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);