包含不止一条信息的数组 - Javascript

Array with more than one piece of information - Javascript

我有这个 javascript 代码:

var randomwordz = [

{word: 'Sweets', image: 'sweets.jpg'}, 
{word: 'Chocolate', image: 'chocolate.jpg'}, 
{word: 'Snow', image: 'snow.jpg'},
{word: 'Pineapple', image: 'pineapple.gif'},
{word: 'Juice', image: 'j.jpg'},
{word: 'Egg', image: 'egg.gif'},
{word: 'Cheese', image: 'cheese.gif'},
{word: 'Chicken', image: 'chick.jpg'}, 
{word: 'Veg', image: 'veg.gif'}];

randomWords[0].word
randomWords[0].image

randomWords[1].word
randomWords[1].image

randomWords[2].word
randomWords[2].image

randomWords[3].word
randomWords[3].image

randomWords[4].word
randomWords[4].image

randomWords[5].word
randomWords[5].image

randomWords[6].word
randomWords[6].image

randomWords[7].word
randomWords[7].image

randomWords[8].word
randomWords[8].image

var randoms = randomWords[Math.floor(Math.random()*randomWords.length)];

document.getElementById('randomwords').innerHTML = randoms;

我的随机词显示在我的页面上,但是自从我在显示随机词时将图像添加到数组后,它显示 - [object Object] 而不是我的随机词。

有谁知道为什么以及如何只显示随机单词。我需要为我的代码的下一部分配对单词和图像。

请原谅我缺乏知识我是新手Javascript

您有一个 Array of Objects,您现在需要从 [=13] 中选择您想要的 属性 =]Object 选择索引后

document.getElementById('randomwords').innerHTML = randoms.word;

// and you may want to do something like this for your image
document.getElementById('randomimages').src= randoms.image;