如何在Collection Meteor Mongo中迭代对象

时间:2015-06-28 01:43:45

标签: mongodb meteor iteration

如果我进行以下搜索:

    <!DOCTYPE html>
    <html>
        <head>
            <title>TODO supply a title</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
            <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
     <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
        </head>
        <body style="background-color: lightblue;">                        
            <form><label>I AM IN</LABEL><select id="size0" class="size0">
                            <option>PLEASE SELECT
                            </option>
                            <option value="1">
                                UK
                            </option>
                            <option value="0">
                                US
                            </option>


                </select><br>

                <label>size to convert </label>
                <select id="size1" class="size1">
                            <option>PLEASE SELECT
                            </option>


                </select><br>
                <label>IS IN </label>
                <select class="size2" id="size23">
                  <option>PLEASE SELECT</option>
                            <option value="1"> UK</option>
  <option value="0"> US</option>
</select><br>


         <button id="butt" type="button" >GET SIZE</button>

            </form>
            <div id="blobla"></div>
            <div id="blobla2"></div> 
        </body>
    </html>

我得到以下内容:

VideoCourses.find({'_id': 'jkwehrjewrew'}).fetch()[0].videos

我的问题是,如何迭代整个集合。我想一次渲染一个视频,所以我可以渲染第一个视频,然后点击,渲染第二个视频等。

问题是,我有很多组视频,所以这应该动态完成。它应该遍历下一个video.first,video.second ..等等。

这可能吗?我已经查看了.next(),但我认为这适用于整个集合,而不是集合中的对象。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

这是一个JavaScript问题,不一定是Meteor。使用for-in循环迭代对象。

// This query returns an object
var MyObject = VideoCourses.find({'_id': 'jkwehrjewrew'}).fetch()[0].videos

// The object is brings back looks like this:
// {    
//  first: "firstvideo.html", 
//  second: "secondvideo.html", 
//  third: "thirdvideo.html"
// };


// Use a for-in loop to iterate through the object
for(key in myObject){
    console.log("this is the key: ", key, ", and this is the value: ", myObject[key]);
};
相关问题