简体   繁体   中英

Looping Objects of Object with handlebars.js

I have a simple node.js app that returns the following json.

{
  computers: {
   john: {
     cpu: "intel",
     ram: "8MB",
     hd: "1TB"
   },
   jane: {
     cpu: "intel",
     ram: "12MB",
     hd: "500GB"
   },
   mary: {
     cpu: "intel",
     ram: "8MB",
     hd: "500GB"
   }
  }
}

in my index.hbs file I have the following.

{{#each computers}}
   {{#each this}}
      {{cpu}} {{ram}} {{hd}}
   {{/each}}
{{/each}}

I would like the get the following result.

John: intel, 8MB, 1TB jane: intel, 12MB, 500GB etc..

Any suggestion is greatly appreciated!

You don't have to create nested each . Simply using paths combined with @key to get current key of iterating object will do.

{{#each computers}}
  {{@key}}: {{./cpu}}, {{./ram}}, {{./hd}}
{{/each}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM