难以理解和创建动态 JavaScript 对象

发布于 2024-09-26 23:42:44 字数 1477 浏览 3 评论 0原文

我希望我的代码能够解释自己,我已经快到了,我只是卡在如何将可选数据合并到具有多个级别的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-10-03 23:42:44

如果我没记错的话,您想要这样的东西:

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.playlistvideo.clip.url)。切勿使用 for ( ... in ...) 结构遍历数组!

If I am not mistaken, you want something like this:

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];
}

The corresponding entries (like video.playlist or video.clip.url) are added dynamically. Never traverse arrays with the for ( ... in ...) construct!

心房的律动 2024-10-03 23:42:44
  1. 不要使用 for-in 数组
  2. 使用 push 方法向数组添加新元素(您使用的格式仅存在在 PHP 中)

代码:

// 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;
  1. don't use for-in for arrays
  2. use push method to add new elements to an array (the format you used only exists in PHP)

Code:

// 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文