难以理解和创建动态 JavaScript 对象
我希望我的代码能够解释自己,我已经快到了,我只是卡在如何将可选数据合并到具有多个级别的 JSON 对象中(是这样吗?)
//get video list - example: video1.flv;test23.flv;Grabledable.flv
var files = j('.videos').html().split(';');
// assume first we have one video, so no need of playlist
var isplaylist = false;
// check if there are actually more than one video
if (files.length > 1) {
isplaylist = true;
var playlist = new Array(); // is this right? I want to add this to the video var
for (var i in files) {
playlist[] = {
url: files[i],
title: 'Video ' + i
};
}
};
//here's where the trouble starts
var video = {
plugins: {
controls: {
playlist: isplaylist,
//i cut out all irrelevant extra data
}
},
clip: {
url: files[0],
// ONLY DO THIS IF isplayer == false;
wmode: 'opaque',
baseUrl: "/video/",
autoPlay: false
},
// from here on this should only occur if isplayer == true;
// following is the way I want it attached from var playlist
playlist: [{
url: 'video1.flv',
title: 'Video 0'
},
{
url: 'test23.flv',
title: 'Video 1'
},
{
url: 'Grabledable.flv',
title: 'Video 2'
}]
};
我的目标是此处列出的格式:< JavaScript 编码部分下的 href="http://flowplayer.org/plugins/javascript/playlist.html" rel="nofollow">http://flowplayer.org/plugins/javascript/playlist.html 。
I hope my code can explain itself, I am almost there, I just am stuck on the way how to merge optional data into a JSON object with multiple levels (is that even what it is?)
//get video list - example: video1.flv;test23.flv;Grabledable.flv
var files = j('.videos').html().split(';');
// assume first we have one video, so no need of playlist
var isplaylist = false;
// check if there are actually more than one video
if (files.length > 1) {
isplaylist = true;
var playlist = new Array(); // is this right? I want to add this to the video var
for (var i in files) {
playlist[] = {
url: files[i],
title: 'Video ' + i
};
}
};
//here's where the trouble starts
var video = {
plugins: {
controls: {
playlist: isplaylist,
//i cut out all irrelevant extra data
}
},
clip: {
url: files[0],
// ONLY DO THIS IF isplayer == false;
wmode: 'opaque',
baseUrl: "/video/",
autoPlay: false
},
// from here on this should only occur if isplayer == true;
// following is the way I want it attached from var playlist
playlist: [{
url: 'video1.flv',
title: 'Video 0'
},
{
url: 'test23.flv',
title: 'Video 1'
},
{
url: 'Grabledable.flv',
title: 'Video 2'
}]
};
My target is the format listed here: http://flowplayer.org/plugins/javascript/playlist.html under the section JavaScript coding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没记错的话,您想要这样的东西:
动态添加相应的条目(例如
video.playlist
或video.clip.url
)。切勿使用for ( ... in ...)
结构遍历数组!If I am not mistaken, you want something like this:
The corresponding entries (like
video.playlist
orvideo.clip.url
) are added dynamically. Never traverse arrays with thefor ( ... in ...)
construct!代码:
Code: