如何在js模板引擎中输出对象的参数名称(小胡子,下划线)

发布于 2024-12-02 05:10:57 字数 406 浏览 4 评论 0原文

我们有一些对象数组:

data = [
{
'showname-array': [
    {'a':..}
    {'b':..}
    {'c':..}
]
},
{
'andanotherName-array': [
    {'a':..}
    {'b':..}
    {'c':..}
]
},
]

是否可以使用 Mustache 或 underscore.js-templates 渲染对象属性的名称: '显示名称数组' 'andanotherName-array'

<div> Hello , showing content of: <% showname-array %> </div> 

怎么可能?

We have some array of objects:

data = [
{
'showname-array': [
    {'a':..}
    {'b':..}
    {'c':..}
]
},
{
'andanotherName-array': [
    {'a':..}
    {'b':..}
    {'c':..}
]
},
]

Is it possible to render with mustache or underscore.js-templates the name of object's property:
'showname-array'
'andanotherName-array'

<div> Hello , showing content of: <% showname-array %> </div> 

How is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2024-12-09 05:10:58

您可以使用下划线_.keys()函数

temp = "<% _.each(_.keys(data), function(name){ %> 
        <div>Hello, showing content of '<%= name %>'</div>
        <% }); %>"
_.template(temp, data); // <div>Hello, showing content of 'showname-array'</div>
                        // <div>Hello, showing content of 'andanotherName-array'</div>

You can use Underscore _.keys() function

temp = "<% _.each(_.keys(data), function(name){ %> 
        <div>Hello, showing content of '<%= name %>'</div>
        <% }); %>"
_.template(temp, data); // <div>Hello, showing content of 'showname-array'</div>
                        // <div>Hello, showing content of 'andanotherName-array'</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文