NodeJS [SOLVED]: Looping Objects of Object with handlebars.js

NodeJS [SOLVED]: Looping Objects of Object with handlebars.js

Home Forums Scripting NodeJS Tutorials NodeJS [SOLVED]: Looping Objects of Object with handlebars.js

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245845

    Cloudy Point
    Keymaster

    QuestionQuestion

    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!

    #245846

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    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}}
    

    Source: https://stackoverflow.com/questions/48014528/looping-objects-of-object-with-handlebars-js
    Author: Chay22
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.