gpt4 book ai didi

javascript - 在 Handlebars.js 中引用数组项

转载 作者:行者123 更新时间:2023-11-28 01:01:20 25 4
gpt4 key购买 nike

我有一个从 CSV 创建的 json,如下所示,

[["Year","Make","Model","Description","Price"],["1997","Ford","E350","ac, abs, moon","3000.00"],["1999","Chevy","Venture \"Extended Edition\"","","4900.00"],["1999","Chevy","Venture \"Extended Edition, Very Large\"","","5000.00"],["1996","Jeep","Grand Cherokee","MUST SELL!\nair, moon roof, loaded","4799.00"]] 

我需要在使用 Handlebars.js 的模板中使用它,当 json 像这样时我知道何时使用它

[{"data" : "data1"},{.....}] 

但是当 json 看起来像我一开始给出的那样时,当我尝试以下代码时,不知道如何使用 Handlebars ,

        {{#each this}}
<li>{{0}}</li>
{{/each}}

我在控制台收到以下错误

Uncaught Error: Parse error on line 3:
...} <li>{{0}}</li>
----------------------^
Expecting 'ID', 'DATA', got 'NUMBER'

无论如何要访问模板的json数据,错误是模板还是json?我该如何修复它?

最佳答案

您需要用方括号括起来:

<ul>
{{#each this}}
<li>{{[0]}}</li>
{{/each}}
</ul>

演示:http://jsbin.com/teduxeduvoqi/1/edit

<小时/>

引用自 the docs :

To reference a property that is not a valid identifier, you can use segment-literal notation, [

关于javascript - 在 Handlebars.js 中引用数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25579954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com