Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  60] [ 7]  / answers: 1 / hits: 141098  / 13 Years ago, mon, january 30, 2012, 12:00:00

Is there a Mustache / Handlebars way of looping through an object properties?


So with


var o = {
bob : 'For sure',
roger: 'Unknown',
donkey: 'What an ass'
}

Can I then do something in the template engine that would be equivalent to


for(var prop in o)
{
// with say, prop a variable in the template and value the property value
}

?


More From » iteration

 Answers
82

Built-in support since Handlebars 1.0rc1



Support for this functionality has been added to Handlebars.js, so there is no more need for external helpers.



How to use it



For arrays:



{{#each myArray}}
Index: {{@index}} Value = {{this}}
{{/each}}


For objects:



{{#each myObject}}
Key: {{@key}} Value = {{this}}
{{/each}}


Note that only properties passing the hasOwnProperty test will be enumerated.


[#87738] Saturday, January 28, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antonb

Total Points: 424
Total Questions: 104
Total Answers: 101

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;