Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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?

share|improve this question
1  
It's not an array of arrays, it's an object of arrays. Have a look here - stackoverflow.com/questions/2532218/… –  Mark Walters Feb 11 '14 at 9:09

4 Answers 4

up vote 5 down vote accepted

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);
}
share|improve this answer
    
Do you mean var ArrOfarr = [{name: A1, data: ["choice", "choice2", "choice3"] }, /* ... */}]; –  M.K Feb 11 '14 at 9:28
    
@M.K What do you mean? –  Sirko Feb 11 '14 at 9: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 9:39
    
@M.K Sorry - fixed some typos and mistakes. Should be working now. –  Sirko Feb 11 '14 at 9: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 9:47
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.

share|improve this answer

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.

share|improve this answer
    
ArrOfarr.length will be undefined => error. –  dfsq Feb 11 '14 at 9:13
    
@dfsq: You're right. Fixed. –  Cerbrus Feb 11 '14 at 9:13
    
This code is not popping up anything –  M.K Feb 11 '14 at 9:31
    
@M.K: How about now? –  Cerbrus Feb 11 '14 at 9:53

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.