handlebars: how to access an array?

2019-06-26 01:11发布

问题:

I have the following simplified document:

{
    channel:'Channelname',
    users: [
        {userId:1},
        {userId:2},
        {userId:3}
    ]
}

How can i access the userId's in a {{#each}} loop like so:

{{#each channels}}
    {{channel}}
    {{#each channels.users}}
        {{userId}} //or {{channels.users.userId}} ?
    {{/each}}
{{/each}}

The first {{#each}} loop prints my channelname as expected, but the second {{#each}} loop doesn't print anything.

Regards, Cid

回答1:

Use

{{#each channels}}
    {{channel}}
    {{#each users}}
        {{userId}}
    {{/each}}
{{/each}}

When going into an each loop, handlebars will use the key names in the array directly.