我希望我的代码可以解释自己,我几乎就在那里,我只是坚持如何将可选数据合并到具有多个级别的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'
}]
};
我的目标是此处列出的格式:http://flowplayer.org/plugins/javascript/playlist.html在JavaScript编码部分。
答案 0 :(得分:1)
如果我没弄错的话,你想要这样的事情:
var video = {
plugins: {
controls: {
playlist: false
}
},
clip: {
wmode: 'opaque',
baseUrl: "/video/",
autoPlay: false
}
}
// check if there are actually more than one video
if(files.length > 1){
video.plugins.controls.playlist = true;
var playlist = [];
for ( var i = 0, l = files.length; i < l; i++ )
{
playlist.push({url: files[i], title: 'Video '+ i});
}
video.playlist = playlist;
}
else {
video.clip.url = files[0];
}
动态添加相应的条目(如video.playlist
或video.clip.url
)。永远不要使用for ( ... in ...)
构造遍历数组!
答案 1 :(得分:1)
代码:
// assuming you have video declared before
var video = { ... };
//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();
for ( var i = 0; i < files.length; i++ ) {
playlist.push ({
url: files[i],
title: 'Video ' + i
});
}
};
video.playlist = playlist;