【问题标题】:How to Iterate through Object inside Collection Meteor Mongo如何遍历 Collection Meteor Mongo 中的对象
【发布时间】:2015-06-29 09:02:56
【问题描述】:

如果我进行以下搜索:

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

我得到以下信息:

Object {first: "firstvideo.html", second: "secondvideo.html", third: "thirdvideo.html"}

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

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

这可能吗?我已经研究过 .next() 但我认为这适用于整个集合,而不是集合内的对象。

提前谢谢你!

【问题讨论】:

  • 你试过什么?能否展示一下你的模板 + 辅助代码以及哪些不起作用,以便我们有一个起点?

标签: mongodb meteor iteration


【解决方案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]);
};

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 2016-04-23
    • 2015-06-13
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 2022-01-24
    相关资源
    最近更新 更多