迭代地图Handelbars

时间:2016-02-23 10:34:48

标签: javascript handlebars.js

我尝试用JSON响应生成一个表

我知道如何访问密钥,但我找不到获取价值的方法

{{#each this}}
  {{@key}}
{{/each}}

每个键都是一行,值将代表行的内容(如下图所示)

回复如下:

这样:

{  
   "DistributedQueue-0":{  
      "MessagesCurrentCount":"0",
      "MessagesPendingCount":"0",
      "MessagesReceivedCount":"0",
      "MessagesHighCount":"0",
      "ConsumersCurrentCount":"0",
      "ConsumersHighCount":"0",
      "ConsumersTotalCount":"0"
   },
   "PatientNotificationQueue":{  
      "MessagesCurrentCount":"0",
      "MessagesPendingCount":"0",
      "MessagesReceivedCount":"0",
      "MessagesHighCount":"0",
      "ConsumersCurrentCount":"0",
      "ConsumersHighCount":"0",
      "ConsumersTotalCount":"0"
   },
   "Topic-0":{  
      "MessagesCurrentCount":"0",
      "MessagesPendingCount":"0",
      "MessagesReceivedCount":"0",
      "MessagesHighCount":"0",
      "ConsumersCurrentCount":"0",
      "ConsumersHighCount":"0",
      "ConsumersTotalCount":"0"
   }
}

enter image description here

由于

1 个答案:

答案 0 :(得分:1)

每个迭代中元素的值在{{this}}之类的块中以{{#each}}形式提供。所以

{{#each this}} 
 {{@key}} = {{this}} 
{{/each}}

在你的情况下,它会呈现类似

的东西
KeyName: [object Object] 

因为{{this}}现在指的是值,它本身就是一个对象。您知道如何使用{{#each}}迭代对象 - 您刚刚完成了。现在你只需要使用嵌套的{{#each}}循环! :)

{{#each this}}
  {{@key}}: 
    {{#each this}}
      {{@key}}: {{this}}
    {{/each}}
{{/each}}

演示:http://jsfiddle.net/f5feae43/2/