Morales
Morales

Reputation: 148

How to limit returned elements from an array from user's collection in Meteor.js?

this is my code:

    return Meteor.users.findOne({
             privNumber: this.privNo
           }).friends;

In the HTML I need to show only 5 the newest elements from an friends array. I tried to push elements into another array, but then I was making an array of an arrays and it wasn't the way I need it to be.

I am looking for a proper way to do it.

Thank you for any help!

Upvotes: 0

Views: 136

Answers (1)

sdooo
sdooo

Reputation: 1881

You would need to do something like

var friends = Meteor.users.findOne({privNumber: this.privNo}).friends;
//some sorting depending on how your object looks like
return friends.slice(0, 4);

It is assuming you have 5 friends in array, if not you can try

return (friends.length >= 5) ? friends.slice(0, 4) : friends;

Upvotes: 1

Related Questions