我有以下javascript

 var wordselection = [
{ 'word': "Pomme", "gender": "m", },
{ "word": "Banane", "gender": "f", },
{ "word": "Ananas", "gender": "m", },
{ "word": "Chat", "gender": "f", },
{ "word": "Chien", "gender": "m", },
{ "word": "Poisson", "gender": "f", },

];

function randomword(){
var chosen = wordselection[Math.floor(Math.random()*wordselection.length)];
document.getElementById("word").innerHTML=chosen;
}


我该如何编码,使其仅显示一个随机的“单词”(例如pomme,banane,ananas)或“性别”

目前它说[object Object](因为它是错误的)

谢谢

最佳答案

尝试:

var chosen = wordselection[Math.floor(Math.random()*wordselection.length)].word

关于javascript - 从随机数组获取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19252665/

10-13 02:46