gpt4 book ai didi

javascript - mustache .js : Enumerable objects with outer variable

转载 作者:行者123 更新时间:2023-12-01 02:33:32 25 4
gpt4 key购买 nike

我已经完成了this很棒的教程。

var data = {
employees: [
{ firstName: "Christophe",
lastName: "Coenraets"},
{ firstName: "John",
lastName: "Smith"}
]};
var template = "Employees:<ul>{{#employees}}" +
"<li>{{firstName}} {{lastName}}</li>" +
"{{/employees}}</ul>";
var html = Mustache.to_html(template, data);
$('#sampleArea').html(html);

我的问题是如何迭代属于员工对象内的数据对象的某些属性?

这是一个例子,我想打印 label每一个 employees 旁边收盘前</li>标签:

var data = {
employees:
[{
firstName: "Christophe",
lastName: "Coenraets"
}],
label: "red"
};

var template = "Employees:<ul>{{#employees}}" +
"<li>{{firstName}} {{lastName}}" +
"the label is: {{label}}" +
"</li>" +
"{{/employees}}</ul>";

但是上面的例子显然行不通,因为 labeldata 的属性(property),不是 employees .

最佳答案

你可以尝试这个:使用“../”向上一级

var template = "Employees:<ul>{{#employees}}" +
"<li>{{firstName}} {{lastName}}" +
"the label is: {{../label}}" +
"</li>" +
"{{/employees}}</ul>";

另一种解决方案是像这样渲染整个节点:

var template = "{{#data}}Employees:<ul>{{#data.employees}}" +
"<li>{{data.employees.firstName}} {{data.employees.lastName}}" +
"the label is: {{data.label}}" +
"</li>" +
"{{/data.employees}}</ul>{{/data}}"

关于javascript - mustache .js : Enumerable objects with outer variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48131120/

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