I have an application that will be receiving arbitrary JSON structures from a server and displaying them on a page, so I need a nice way to enumerate to any depth over the key/value pairs in a map. I’ve looked at using the each
handlebars helper directly on the object in conjunction with the special @key
helper but this does not work.
I’ve also taken a look at solutions like this but they seem scrappy, especially for something that should be appearing regularly in the code.
Thanks
1 Like
I don’t think this one is scrappy:
var objKeys = []
for(var key in obj) {
if(obj.hasOwnProperty(key)){
objKeys.push(key);
}
}
Though I would definitely make it into a function and store it under App.Utils or something, since, like you said, it will be appearing regularly.
Right, yeah. In and of itself it’s not a scrappy way to get the keys from an object, but how can I then use these keys to bind to values in a template.
For example if I expose an object and it’s keys on a controller, then in a template I would want to do something like:
{{#each key in keys}}
{{object[key]}}
{{/each}
This won’t work, but I’m looking for a way to achieve this behaviour. I feel like this should be simpler than it is and am missing something simple here.