6

I have an array of arrays.

var ArrOfarr = { A1: ["choice", "choice2", "choice3"], A2: ["choice1", "choice2"], A3: ["choice1", "choice2"], A4: [], A5: [], A6: [], A7: [] }

I want to pick random array from the 'ArrOfarr' each time I click a button. I tried the below, but seeing 'undefined':

function A()
{
var item = ArrOfarr[Math.floor(Math.random()*ArrOfarr.length)];
alert(item);
}

how can I pick up random array form the above Array(Without repeat till it reaches its length). And how can I get the name of randomly picked array?

M.K
  • 171
  • 1
  • 12
  • 1
    It's not an array of arrays, it's an object of arrays. Have a look here - http://stackoverflow.com/questions/2532218/pick-random-property-from-a-javascript-object – Mark Walters Feb 11 '14 at 09:09

4 Answers4

5

You do not have an array there, but an object containing arrays. To select a random entry, you could use this code:

function A(){
  var keys = Object.keys( ArrOfarr );

  var name = keys[ Math.floor(Math.random()*keys.length) ];

  var item = ArrOfarr[ name ];

  alert( name );
  alert( item );
}

An alternative would be to change you data structure in the first place: Instead of arrays as inner object, you could use a wrapper object, that contains data as well as name.

var ArrOfarr = [  {name: 'A1', data: ["choice", "choice2", "choice3"] }, /* ... */ ];

function A() {
  var item = ArrOfarr[Math.floor(Math.random()*ArrOfarr.length)];
  alert(item.data);
}
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • Do you mean var ArrOfarr = [{name: A1, data: ["choice", "choice2", "choice3"] }, /* ... */}]; – M.K Feb 11 '14 at 09:28
  • @M.K What do you mean? – Sirko Feb 11 '14 at 09:37
  • Any of the above solutions is working for you? I tried both but didn't see the alert. – M.K Feb 11 '14 at 09:39
  • @M.K Sorry - fixed some typos and mistakes. Should be working now. – Sirko Feb 11 '14 at 09:41
  • Thank you Sriko, working now. But random pick is happening like A3,A5,A3,A7.. How to prevent to do not pick the already picked up one before it finishes picking up all the distinct arrays. – M.K Feb 11 '14 at 09:47
  • @M.K Have some kind of "working" array: Initialize it with all items and then remove the entries already picked until it is all empty. Then reinitialize it and go on. – Sirko Feb 11 '14 at 09:48
1
var ArrOfarr = { A1: ["choice", "choice2", "choice3"], A2: ["choice1", "choice2"], A3: ["choice1", "choice2"], A4: [], A5: [], A6: [], A7: [] }

is a object literal

var ArrOfarr = [["choice", "choice2", "choice3"], ["choice1", "choice2"], ["choice1", "choice2"], [],  [], [],  []]

array of arrays;

Either change your code with array definition, or use Sirko's code instead.

Tudor Zgureanu
  • 725
  • 7
  • 11
1

You just need to prefix 'A' to the index you're randomly picking:

function A(){
    var item = ArrOfarr['A'+(Math.floor(Math.random() * ArrOfarr.keys().length)+1)];
    alert(item);
}

Also, notice the +1: That's because your object starts counting at A1, while your random function returns values starting at 0.

The last change is the addition of .keys() in your random function. Because ArrOfarr is a object, it doesn't have a length property. However, .keys() returns a array of it's keys, which has a length property.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Count the number of keys in the object, ran a random operation with upper bound of that count.

Use the for..in loop to get to that random number, +1 for every property. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

alexK85
  • 93
  • 6