【发布时间】:2014-10-10 09:35:29
【问题描述】:
我想拥有一个包含 10 个不同配方的数组,在每个页面加载时,我想用其中一个配方填充一个 div,我是数组新手,但我不确定为什么这里没有任何警报?
var recipes = [{
"image": "image1.jpg",
"ingredients": {
"a": "eggs",
"b": "chips"
},
"link" : "google.com"
}, {
"image": "image2.jpg",
"ingredients": {
"a": "chocolate",
"b": "cheese"
},
"link" : "msn.com"
}];
var recipe = recipes[0];
alert(recipe);
【问题讨论】:
-
我得到
[object Object],这是alert在被要求显示JS对象时的正常输出。 -
recipes[0].image是你想要的 a href 吗? -
获取随机数组对象使用
var randomRecipe = recipes[Math.floor(Math.random() * recipes.length)];
标签: javascript jquery arrays