admin管理员组

文章数量:1302937

In mustache if we have an array like :

var a = [1,2,3,4];

We can create template like:

{{#a}}
{{.}}
{{/a}}

to iterate over it. Now if we have some thing like

var a = [[1,2], [3,4], [5,6]]

Can we create a template like:

{{#a}}
key is {{0th element}} and the value is {{1st element}}
{{/a}}

In mustache if we have an array like :

var a = [1,2,3,4];

We can create template like:

{{#a}}
{{.}}
{{/a}}

to iterate over it. Now if we have some thing like

var a = [[1,2], [3,4], [5,6]]

Can we create a template like:

{{#a}}
key is {{0th element}} and the value is {{1st element}}
{{/a}}
Share edited Mar 17, 2012 at 8:39 aditya_gaur asked Mar 6, 2012 at 11:25 aditya_gauraditya_gaur 3,2957 gold badges34 silver badges43 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Tried out things and got the solution: We can do the following:

var htm = '{{#names}}'+
            '<p> value="{{0}}" key = "{{1}}"</p>'+
          '{{/names}}';
ich.addTemplate('formNameOptionsHTML',htm);
var arr =[[0,1],[10,11],[20,21]];
var htm = ich.formNameOptionsHTML({names:arr});
$('body').append(htm);

Here is the jsfiddle link to it.

本文标签: javascriptMustache(icanhaz) iterating array of arraysStack Overflow