Handlebars.js:从循环中索引访问 global.json。

问题描述 投票:0回答:1

我有一个 global.json 文件。

{
    "title": "problem",
    "dict": [
        {
            "name": "A",
            "similar": [1, 2]
        },
        {
            "name": "B",
            "similar": [0]
        },
        {
            "name": "C",
            "similar": [1]
        },
     ]
}

我想得到以下结果 A 作为一个例子。

A
B C

我试着用下面的车把式脚本。

{{#with global.dict.[0]}}

{{name}}

{{#each similar}}
   {{@root.global.dict.[this].name}}
{{/each}}
{{/with}}

输出的结果是... A. 然而,似乎 this 在这里不被识别为整数,以作为索引使用

json handlebars.js
1个回答
1
投票

你将需要使用查找助手来实现 查找 活力四射 this 指数 @root.global.dict. 然后,你将不得不进行第二次查找,以获得的。name 属性的结果。或者,你也可以使用 助手,为你提供一个覆盖到第一次查找结果的块,如。

{{#each similar}}
    {{#with (lookup @root.global.dict this)}}
        {{name}}
    {{/with}}
{{/each}}

我已经创建了一个 抚弄 供大家参考。

© www.soinside.com 2019 - 2024. All rights reserved.