[英]Pick random item from array and populate - jQuery
Id like to have an array containing 10 different recipes, on each page load i want to populate a div with one of these recipes, Im new to arrays but im not sure why nothing is alerting here? 我想有一个包含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);
http://jsfiddle.net/6otvzr1p/1/ http://jsfiddle.net/6otvzr1p/1/
Actually you are getting the correct value only . 实际上,您只是获得正确的值。 Parse the recipes object and put the required value .
解析配方对象并放入所需的值。 You can use this code to get the random number between 0 and 9 which can be passed as index to array
您可以使用此代码获取0到9之间的随机数,该随机数可以作为索引传递给数组
Math.floor((Math.random() * 10))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.