最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I choose an object key at random? - Stack Overflow

programmeradmin8浏览0评论

I have the following code;

namedarray['India']='New Delhi';
namedarray['Australia']='Canberra';
namedarray['Indonasia']='Jakarta';
namedarray['Iran']='Tehrani';
namedarray['Iraq']='Bhagdad';
namedarray['Nijeria']='Abuja';

document.getElementById('question').innerHTML="Q." +namedarray['Nepal']+"  is capital for which country";

In place of Nepal, I want to choose a key from the object at random. How can I do this?

I have the following code;

namedarray['India']='New Delhi';
namedarray['Australia']='Canberra';
namedarray['Indonasia']='Jakarta';
namedarray['Iran']='Tehrani';
namedarray['Iraq']='Bhagdad';
namedarray['Nijeria']='Abuja';

document.getElementById('question').innerHTML="Q." +namedarray['Nepal']+"  is capital for which country";

In place of Nepal, I want to choose a key from the object at random. How can I do this?

Share Improve this question edited Jul 10, 2011 at 21:03 Andy E 345k86 gold badges481 silver badges451 bronze badges asked Dec 13, 2010 at 10:46 MihirMihir 8,73418 gold badges58 silver badges88 bronze badges 2
  • 6 OBJECTS they're called OBJECTS associative Arrays are ordered JavaScript Objects are unordered, that's an important difference... where are my pills.. – Ivo Wetzel Commented Dec 13, 2010 at 10:56
  • 3 @Ivo I've got weed if you like... :) – Jacob Relkin Commented Dec 13, 2010 at 11:14
Add a ment  | 

3 Answers 3

Reset to default 4

Try this:

function fetch_random(obj) {
    var temp_key, keys = [];
    for(temp_key in obj) {
       if(obj.hasOwnProperty(temp_key)) {
           keys.push(temp_key);
       }
    }
    return obj[keys[Math.floor(Math.random() * keys.length)]];
}

var random_name = fetch_random(namedarray);
document.getElementById('question').innerHTML="Q." + random_name +"  is capital for which country"

If you are capable of using libraries, you may find that Lo-Dash JS library has lots of very useful methods for such cases. In this case, go ahead and check sample().

(Note Lo-Dash convention is naming the library object _. Don't forget to check installation in the same page to set it up for your project.)

_.sample([1, 2, 3, 4]);
// → 2

In you case, go ahead and use:

_.sample(namedarray)

and in context:

document.getElementById('question').innerHTML="Q." +_.sample(namedarray)+"  is capital for which country";

On a side note, you could use a simpler notation for populating the array.

namedarray = {
    India : 'New Delhi',
    Australia : 'Canberra',
    Indonasia : 'Jakarta',
    Iran : 'Tehrani',
    Iraq : 'Bhagdad',
    Nijeria : 'Abuja'
}

I would just use two arrays for the data.

var countries = ['India', 'Australia', 'Indonasia', ... ];
var capitols = ['New Delhi', 'Canberra', 'Jakarta', ...];

Then insert into your text with:

var index = Math.floor(Math.random() * capitols.length);
document.getElementById('question').innerHTML="Q." +capitols[index]+"  is capital for which country";

You can then use the index variable to look up the answer later on as well.

发布评论

评论列表(0)

  1. 暂无评论