0

我们有一些对象数组:

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

是否可以用 mustache 或 underscore.js-templates 渲染对象属性的名称:'showname-array''andanotherName-array'

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

这怎么可能?

4

1 回答 1

1

您可以使用下划线_.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>
于 2011-09-15T10:17:38.543 回答