How to find Array length inside the Handlebar templates?

后端 未结 4 1055
误落风尘
误落风尘 2021-02-01 11:43

I have a Handlebars template which is rendered using a json object. In this json I am sending an array. Like this:

var json = {
               \"array\":[\"abc\"         


        
4条回答
  •  自闭症患者
    2021-02-01 12:22

    In this case you need to reference the parent variable of the each from within the each block:

    {{#each array}}
        {{../array.length}}
    {{/each}}
    

    I think your variable being named "array" is probably conflating the issue as well. Let's assume some different JSON just to clarify:

    var json = {
        "fruit":["apple","orange","banana"]
    };
    

    So then doing this:

      {{#each fruit}}
    • {{this}} {{@index}} {{../fruit.length}}
    • {{/each}}

    Would yield:

    • apple 0 3
    • orange 1 3
    • banana 2 3

提交回复
热议问题