Search code examples
javascriptdictionarymeteorskip

How would I use the cursor.map function to skip one of the the first item in a collection


I have the following template helper for outputting the first {{url}} and all the {{thumb}} however I want to skip the first item if the {{thumb}} not sure how to do this in the map function.

Template.motionPictures.helpers({
  posts: function() {

  return  Posts.find({}, {fields: {thumb: 1, url: 1}}).map(function(post, index) {
  if (index === 0) {
    return post;
  } else {
    delete post.url;
    return post;
  }
});

}
});

When I console.log the posts array, I get the following

enter image description here

This is what I expected but I want the first object in the array to only show its {{url}} not is {{thumb}} so far all the suggestions just delete the object in total, or not at all.


Solution

  • Ok so you both had good advice but I didn't realize what I had to do until I did that console .log.

    The following code worked just as I wanted it to

    Template.motionPictures.helpers({
      posts: function() {
       return Posts
            .find({}, {fields: {thumb: 1, url: 1}, sort: {createdAt: -1}})
            .map(function(post, index) {
              if (index === 0) {
    
                delete post.thumb;
                return post;
    
    
              } else {
                delete post.url;
                return post;
              }
            });
        }
    });