问题
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