I am using Mustache and using the data
{ \"names\": [ {\"name\":\"John\"}, {\"name\":\"Mary\"} ] }
My mustache template is:
As long as you're not traveling into more than one array you can store the index on the helper and just increment when the context changes. I'm using something similar to the following:
var data = {
foo: [{a: 'a', 'b': 'a'}, {'a':'b', 'b':'b'}],
i: function indexer() {
indexer.i = indexer.i || 0;
if (indexer.last !== this && indexer.last) {
indexer.i++;
}
indexer.last = this;
return String(indexer.i);
}
};
Mustache.render('{{#foo}}{{a}}{{i}}{{b}}{{i}}{{/foo}}', data);