Skip to content

Any idea of how to detect the last element in an "each" with an internal "if" #1039

Answered by 255kb
jpeter06 asked this question in Help
Discussion options

You must be logged in to vote

Hi,

The best solution would be to have a filter helper to create an array and then do an each. But we don't have such helper. You can open an issue if you want.

I still found an alternative by creating another variable counting the number of item found:

[ 
  {{setVar 'type' (queryParam 'type' '') }}

  {{setVar 'nb' 0}}

  {{#each (dataRaw 'zones' )}} 
    {{#if (includes this.type @type)}}

    {{setVar 'nb' (add (getVar 'nb') 1)}}

    {{#if (gt @nb 1)}}
      {{#unless @../last}},{{/unless}}
    {{/if}}

    {"label": "{{this.label}}", "value": "{{this.value}}"}

    {{/if}}
  {{/each}}
]

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jpeter06
Comment options

Answer selected by jpeter06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants