ams
ams

Reputation: 62722

How to iterate an array and acces a map using Handlebars?

I have data structures similar to the code below that I want to iterate through with Handlerbars. While the javascript code that can do this iteration is clear, I have not been able to figure out how to do it in handlebars.

var keys = ['key1','key2','key3'] 
var map = {'key1':{...}, 'key2':{...}, 'key3':{...}, .... 'keyN': {...}} 

What I want to do within handlebars is to iterate the keys array and use the value from the keys array to look-up the object from the map. Can this be done without writing a helper?

UPDATE I know how to write the code in javascript, I want to do is "what i can do in raw js using handlebarJS expressions".

Upvotes: 1

Views: 1962

Answers (1)

Shreedhar
Shreedhar

Reputation: 5640

try this :

for(var i=0; i<keys.length; i++){
   console.log(map[keys[i]])
}

Handlebars.registerHelper('list', function(keys, maps) {
  var out = "<ul>";

  for(var i=0, i=keys.length; i++) {
    out = out + "<li>" + map[keys[i]] + "</li>";
  }

  return out + "</ul>";
});

Upvotes: 1

Related Questions