我想显示不同名称的json键。
以下是我要显示为jan,feb,mar等的月份数。
我的尝试如下。
我的结果是1,2,3,4 ...而不是jan,feb,mar ...
我究竟做错了什么?
PS:使用handlebars.js
我的HTML代码:
{{#each this}}
<th style="white-space: nowrap;">{{@key}}</th>
{{/each}}
我的JSON代码:
"PaymentProfile":{
"2017":{
"1":"C",
"2":"C",
"3":"C",
"4":"C",
"5":"C",
"6":"C",
"7":"C",
"8":" ",
"9":" ",
"10":" ",
"11":" ",
"12":" "
},
我的JavaScript:
var months=['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec'];
var mortgagePath=data.TradeLine['Mortgage Accounts']['0'].PaymentProfile;
$.each(mortgagePath,function(k,v){
console.log("year is"+k)
$.each(v, function(k1,v1){
// k1= months[k1];
console.log("month is "+months[k1-1]);
console.log("status is "+v1)
})
});
最佳答案
您能尝试下面的代码对我有用吗?您必须注册一个助手以使用monthname
将计算的属性显示为@key
在您的JS文件中添加
Handlebars.registerHelper('monthName', function(key) {
return months[key-1];
//months is the array which contains the name of months
});
在您的模板中进行如下更改
{{#each this}}
<th style="white-space: nowrap;">{{monthName @key}}</th>
{{/each}}
这是工作提琴的链接
http://jsfiddle.net/az5skvfm/2/
关于javascript - 如何解析 Handlebars 中更改的json数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46454083/