Garrett Smith
Garrett Smith

Reputation: 49

Iterating over and formatting an array field with meteor and mongodb

Say I have mongodb collection called "people", and the following documents are inside the people collection:

{
  name: "James",
  aliases: ["Jim", "Jimmy", "Jimbo", "Jamie"]
}

{
  name: "Mary",
  aliases: ["Mae", "May", "Molly", "Mimi"]
}

I'm trying to find a way to write the aliases array for each document in an HTML element, and also format them with spaces.

Doing this: [EDITED] I had given a bad example

<p>{{aliases}}</p>

results in listing the names like

"Jim,Jimmy,Jimbo,Jamie"

. Is there a way I can make it result in

"Jim, Jimmy, Jimbo, Jamie"

with spaces instead?

Upvotes: 3

Views: 68

Answers (1)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

By using {{aliases}}, the compiler will set the array as value for innerHTML property and the array will be automatically coerced to string.

When we're using .toString() method for one array it's invoked automatically join method with default delimiter = ','

Use this:

{{aliases.join', '}}

Syntax edited

Upvotes: 4

Related Questions