开发者

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

开发者 https://www.devze.com 2023-04-01 00:50 出处:网络
We have some array of objects: data = [ { \'showname-array\': [ {\'a\':..} {\'b\':..} {\'c\':..} ] }, { \'andanotherName-array\': [

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

精彩评论

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