繁体   English   中英

具有多个信息的数组-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不够了解

您有一个对象 数组 ,现在需要在选择索引后从对象中选择所需的属性

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM