How to Iterate through Object inside Collection Meteor Mongo

妖精的绣舞 提交于 2020-01-25 13:20:13

问题


If I do the following search:

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

I get the following:

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.

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.

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

Thank you in advance!


回答1:


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


来源:https://stackoverflow.com/questions/31095341/how-to-iterate-through-object-inside-collection-meteor-mongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!