开发者

How to output object's parameter name in js templating engines (mustache, underscore)

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 i开发者_运维百科t possible?


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜