Sort object keys with Handlebars

佐手、 提交于 2020-01-04 09:14:05

问题


I know that Javascript object keys are not 'ordered' so by definition cannot be sorted – however I would like to output them in alphabetical order using Handlebars for display purposes.

The part of the object I'm interested in looks like this:

{
    "A publisher" : {
        journals : {
            "A journal" : {},
            "B journal" : {},
            "C journal" : {}
        }
    }

    "B publisher" : {
        journals : {
            "D journal" : {},
            "E journal" : {},
            "F journal" : {}
        }
    }
}

I'd like to be able to template the object using Handlebars ordered alphabetically by publisher, then each journal in alphabetical order within each publisher, something like:

{{#each this}}    //iterate over publishers in alphabetical order
    <h2>{{@key}}</h2>
    {{#each journals}}    //iterate over each journal in alphabetical order
    <ul>
        <li>{{@key}}</li> 
    </ul>
    {{//each}}
{{/each}}

I need to use a Handlebars helper for this I think, but am not really sure where to start. Can anyone point me in the right direction?

Thanks!

来源:https://stackoverflow.com/questions/25310889/sort-object-keys-with-handlebars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!