gpt4 book ai didi

javascript - 如何使用每个有 mustache 的 javascript?

转载 作者:数据小太阳 更新时间:2023-10-29 03:59:51 24 4
gpt4 key购买 nike

我有一些 json 对象,其中一些里面有一些其他对象。

如果我只留下里面没有其他对象的 json 对象,然后应用模板,一切顺利,我得到,在本例中有 3 个 li 元素。

但是如果我抓取原始的 json obj,结果会有点连线。我相信我需要做一个 each 语句来从每个主要对象中迭代每个子 json obj

也许我有点困惑所以这里是一些代码。

我有一些像这样的 json 数据:

{
"msg_id":"134",
"message":"Nick",
"comment":[
{
"com_id":"9",
"comment":"test",
},
{
"com_id":"10",
"comment":"testtt",
},
{
"com_id":"11",
"comment":"testtttt",
}]
},
{
"msg_id":"134",
"message":"Nick",
},
{
"msg_id":"134",
"message":"Nick",
}

我正试图达到这样的目的: 缺口

测试

测试

测试

缺口 缺口

我创建了一个这样的模板:

function messagesTamplate(data)
{
$.each(data, function(index, obj)
{
msg += template.replace( /{{message}}/ig , obj.message );
if(obj.comment) {
$.each(obj.comment, function(key, val)
{
msg += template.replace( /{{comment}}/ig , val.comment );
});
}

});

return msg;
}

然后我将其附加到主 ul。

谢谢

最佳答案

data 需要是一个数组(见附件[])

var data = [{
"msg_id": "134",
"message": "Nick",
"comment": [{
"com_id": "9",
"comment": "test",
}, {
"com_id": "10",
"comment": "testtt",
}, {
"com_id": "11",
"comment": "testtttt",
}]
}, {
"msg_id": "134",
"message": "Nick",
}, {
"msg_id": "134",
"message": "Nick",
}]

在 mustache 模板中就是这样:

{{#data}}         //loop through all data
{{message}} //pick out the "message" per iteration
{{#comment}} //loop through all comments in an iterated item
{{comment}} //pick out the comment
{{/comment}}
{{/data}}

关于javascript - 如何使用每个有 mustache 的 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798084/

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