【发布时间】:2020-03-03 20:23:05
【问题描述】:
我正在尝试向用户显示随机横幅。我有两种类型的广告,图片和滑块。我想随机选择其中一个,然后从之前选择的广告类型中随机选择一个广告。所以我想随机获取imageAds 或sliderAds 对象,然后随机获取选定imageAds 或sliderAds 的一个子对象。
这是我的 JSON:
var campaign = {
imageAds: {
imageAd1: {
img: 'imageAd1/imageURL',
button: {
text: 'imageAd1 button text',
link: 'imageAd1 button link',
color: '#fff',
},
headline: {
text: 'imageAd1 headline',
color: '#fff',
},
spots: {
spot1: {
img: 'spot1 img',
text: 'spot1 text',
color: '#fff',
},
spot2: {
img: 'spot2 img',
text: 'spot2 text',
color: '#fff',
},
spot3: {
img: 'spot3 img',
text: 'spot3 text',
color: '#fff',
},
},
footer: {
text: 'imageAd1 footer',
color: '#fff',
},
},
imageAd2: {
img: 'imageAd2/imageURL',
button: {
text: 'imageAd2 button text',
link: 'imageAd2 button link',
color: '#fff',
},
headline: {
text: 'imageAd2 headline',
color: '#fff',
},
spots: {
spot1: {
img: 'spot1 img',
text: 'spot1 text',
color: '#fff',
},
spot2: {
img: 'spot2 img',
text: 'spot2 text',
color: '#fff',
},
spot3: {
img: 'spot3 img',
text: 'spot3 text',
color: '#fff',
},
},
footer: {
text: 'imageAd2 footer',
color: '#fff',
},
},
},
sliderAds: {
sliderAd1: {
slide1: {
img: 'slide1/imageURL',
button: {
text: 'slide1 button text',
link: 'slide1 button link',
color: '#fff',
},
headline: {
text: 'slide1 headline',
color: '#fff',
},
spots: {
spot1: {
img: 'spot1 img',
text: 'spot1 text',
color: '#fff',
},
spot2: {
img: 'spot2 img',
text: 'spot2 text',
color: '#fff',
},
spot3: {
img: 'spot3 img',
text: 'spot3 text',
color: '#fff',
},
},
footer: {
text: 'slide1 footer',
color: '#fff',
},
},
slide2: {
img: 'slide2/imageURL',
button: {
text: 'slide2 button text',
link: 'slide2 button link',
color: '#fff',
},
headline: {
text: 'slide2 headline',
color: '#fff',
},
spots: {
spot1: {
img: 'spot1 img',
text: 'spot1 text',
color: '#fff',
},
spot2: {
img: 'spot2 img',
text: 'spot2 text',
color: '#fff',
},
spot3: {
img: 'spot3 img',
text: 'spot3 text',
color: '#fff',
},
},
footer: {
text: 'slide2 footer',
color: '#fff',
},
},
},
sliderAd2: {
slide1: {
img: 'slide1/imageURL',
button: {
text: 'slide1 button text',
link: 'slide1 button link',
color: '#fff',
},
headline: {
text: 'slide1 headline',
color: '#fff',
},
spots: {
spot1: {
img: 'spot1 img',
text: 'spot1 text',
color: '#fff',
},
spot2: {
img: 'spot2 img',
text: 'spot2 text',
color: '#fff',
},
spot3: {
img: 'spot3 img',
text: 'spot3 text',
color: '#fff',
},
},
footer: {
text: 'slide1 footer',
color: '#fff',
},
},
slide2: {
img: 'slide2/imageURL',
button: {
text: 'slide2 button text',
link: 'slide2 button link',
color: '#fff',
},
headline: {
text: 'slide2 headline',
color: '#fff',
},
spots: {
spot1: {
img: 'spot1 img',
text: 'spot1 text',
color: '#fff',
},
spot2: {
img: 'spot2 img',
text: 'spot2 text',
color: '#fff',
},
spot3: {
img: 'spot3 img',
text: 'spot3 text',
color: '#fff',
},
},
footer: {
text: 'slide2 footer',
color: '#fff',
},
},
},
},
};
目前我能够获得imageAds 或sliderAds,但我无法随机获得他们的子对象。这是我当前的代码:
var properties = Object.getOwnPropertyNames(campaign);
var index = Math.floor(Math.random() * properties.length);
var adType = {};
adType[properties[index]] = campaign[properties[index]];
// output: imageAds or sliderAds object
var properties = Object.getOwnPropertyNames(adType);
var index = Math.floor(Math.random() * properties.length);
var ad = {};
ad[properties[index]] = adType[properties[index]];
// Expected output: imageAd1, imageAd2, sliderAd1 or sliderAd2
// Actual output: imageAds or sliderAds object
console.log(ad);
那么怎么处理呢?
【问题讨论】:
-
有些地方你写的是
propertiesi而不是properties -
@andergtk 抱歉打错了,已修复,但问题仍然存在。代码没有按预期工作。
标签: javascript json object