wyc
wyc

Reputation: 55293

How to loop and output the attributes of more than one object in CoffeeScript?

This is what I'm doing right now:

"click .save-file": (e) ->
  posts = Posts.find().fetch()
  console.log post for post in posts

Which outputs:

Object {_id: "jMraqpqyAcHz9BCM4", title: "Post 1", position: 3}
Object {_id: "7dsT8RpsPZ3LfjisX", title: "Post 3", position: 2}

How can I modify the code above so that it outputs:

Post 1
3

Post 3
2

Instead? (I think I have to do something like: "#{@title}\n\n#{@position}")

Upvotes: 0

Views: 35

Answers (2)

Larry Maccherone
Larry Maccherone

Reputation: 9523

Try...

"click .save-file": (e) ->
  posts = Posts.find().fetch()
  for post in posts
    console.log post.title
    console.log post.position 

Upvotes: 1

squid314
squid314

Reputation: 1395

I think what you need to use is "#{post.title}\n#{post.position}\n" since @ references this and you need to reference post.

Upvotes: 1

Related Questions