0

I'd like to take a random array from a 2D named array but all solution I tried didn't work maybe someone could help

var sites = [];

sites['apple'] = [
'green'
,
'red'
,
'blue'
];

sites['orange'] = [
'yellow'
];

There is more and of course not these names and the number is not fix and I'd like to take one random array. (Not item !)

Is it possible ?

Yoken
  • 1

1 Answers1

0

Solution is :

var sites = [];

sites['apple'] = [
'green'
,
'red'
,
'blue'
];

sites['orange'] = [
'yellow'
];

var s_Array = Object.keys(sites);
var randomNumber = Math.random();
var s_Index  = Math.floor(randomNumber * s_Array.length);
var randomKey = s_Array[s_Index];
var randomsiteskey = sites[randomKey];

Check Pick random property from a Javascript object as @John Montgomery mentionned it

Yoken
  • 1