如何遍历Collection Meteor Mongo中的对象 [英] How to Iterate through Object inside Collection Meteor Mongo

查看:78
本文介绍了如何遍历Collection Meteor Mongo中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我进行以下搜索:

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

我得到以下信息:

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

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

My question, is how can iterate through the collection. I want to render one video at a time, so I can render the first video, and then on click, render the second, etc.

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

The thing is, I have many sets of videos, so this should be done dynamically. It should iterate through the next video.first, video.second.. etc.

有可能吗?我研究了.next(),但我认为它适用于整个集合,而不是集合内的对象.

Is that possible? I've looked into .next() but I think that works for an entire collection, not an object inside the collection.

提前谢谢!

推荐答案

这是一个JavaScript问题,不一定是Meteor.使用for-in循环遍历对象.

This is a JavaScript question, not necessarily Meteor. Use a for-in loop to iterate through objects.

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

这篇关于如何遍历Collection Meteor Mongo中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆