gpt4 book ai didi

mustache .js : Iterate over a list received via json

转载 作者:行者123 更新时间:2023-12-02 16:17:06 24 4
gpt4 key购买 nike

Possible Duplicate:
Mustache JS Template with JSON Collection

我有一个像这样的 json 响应,我想将其与 Mustache.js 一起使用:

[
{"id": "1", "details": {"name": "X", "type":"Y" }},
{"id": "2", "details": {"name": "aName", "type":"something" }}
]

如何使用 Mustache.js 迭代它?

最佳答案

数组本身应该是一个更大的对象中的值,如下所示:

var obj = {
arr: [
{"id": "1", "details": {"name": "X", "type":"Y" }},
{"id": "2", "details": {"name": "aName", "type":"something" }}
]
}

然后你可以这样做:

{{#arr}}
my id: {{id}}
{{/arr}}

{{#bla}} 的使用实际上是重载的。当它作用的属性是一个数组时,它将循环。当属性本身是一个对象时,它将“进入”该对象。给定你的例子:

{{#arr}}
my id: {{id}} <br/>
{{#details}}
and my name: {{name}}
{{/details}}
{{/arr}}

关于 mustache .js : Iterate over a list received via json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11891123/

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