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

编程入门 行业动态 更新时间:2024-10-22 08:36:55
本文介绍了Handlebars.js:从循环中索引对global.json的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个global.json文件:

I have a global.json file:

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

,我希望将A的以下结果作为示例:

and I want the following result for A as an example:

A B C

我尝试使用以下车把脚本:

I tried to use the following handlebars script:

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

输出仅为A.但是,似乎this在这里不能识别为整数用作索引

The output is is just A. However, it seems this is not recognized as integer here to used as the index

推荐答案

您将需要使用查找助手来查找 @root.global.dict上的动态this索引.然后,您将不得不进行第二次查找以获取结果的name属性.另外,您可以使用 with 助手来为您提供范围为第一次查找的结果,如:

You will need to use the lookup helper to lookup the dynamic this index on @root.global.dict. You would then have to do a second lookup to get the name property of the result. Alternatively, you could use the with helper to give you a block scoped to the result of the first lookup, as in:

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

我创建了一个小提琴供您参考.

I have created a fiddle for your reference.

更多推荐

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

本文发布于:2023-11-14 09:23:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   js   Handlebars   global   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!