代码之家  ›  专栏  ›  技术社区  ›  Moak

无法理解和创建动态JavaScript对象

  •  0
  • Moak  · 技术社区  · 16 年前

    我希望我的代码能够自我解释,我就快到了,我只是困在如何将可选数据合并到一个具有多个级别的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编码部分下。

    2 回复  |  直到 16 年前
        1
  •  1
  •   Felix Kling    16 年前

    如果我没弄错,你想要这样的东西:

    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 ...)

        2
  •  1
  •   Community Mohan Dere    9 年前
    1. for-in for arrays
    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;