留着小胡子遍历JSON数组 [英] iterate through JSON array with mustache
本文介绍了留着小胡子遍历JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Mustache的新手,请忍受我:)
I am new to Mustache, please bear with me :)
我的JSON中有一个数组
I have an array in my JSON
"prop":{"brands":["nike","adidas","puma"]}
如果我有这样的模板
{{#prop}}
<b>{{brands}}</b>
{{prop}}
我想得到类似的东西
<b>nike</b>
<b>adidas</b>
<b>puma</b>
我了解数组中的元素不是哈希键/值对,但是我想知道在胡须中是否可以迭代这些元素.
I understand the elements in the array are not hash key-value pairs, however I am wondering if there is anyway in mustache that I can iterate through the elements.
谢谢!
推荐答案
胡子是没有逻辑的,因此不可能编写自己的迭代/循环.不过,转换JSON很容易.例如:
mustache is logicless, so writing your own iteration/loop in it is impossible. It is easy to convert your JSON though. For example:
var json = '{"prop":{"brands":["nike","adidas","puma"]}}';
var obj = JSON.parse(json);
var data = {brands: obj.prop['brands'].map(function(x){ return {name: x}; })};
为您提供一个将与模板一起使用的data
变量:
Gives you a data
variable which will work with the template:
{{#brands}}
<b>{{name}}</b>
{{/brands}}
这篇关于留着小胡子遍历JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文