我是jQuery的新手,希望有人能帮我。我有一个JSON数据,如下
events: function(start, end, tz, callback) {
var events = [];
var num_events = [
{"formId": 9,
"fornCode": "PP40",
"Model": [
{"taxMonth": 0,
"effectiveDate": "2018-10-09"
},
{
"taxMonth": 3,
"effectiveDate": "2018-11-10"
},
{
"taxMonth": 10,
"effectiveDate": "2018-12-19"
}
]
},{"formId": 18,
"fornCode": "PP30",
"Model": [
{"taxMonth": 0,
"effectiveDate": "2018-01-09"
},
{
"taxMonth": 3,
"effectiveDate": "2018-04-10"
},
{
"taxMonth": 10,
"effectiveDate": "2018-11-19"
}
]
}
];
我试着像这个一样循环所有这些数据
$.each (num_events, function (index, object) {
formId = num_events[index].formId,
fornCode = num_events[index].fornCode
$.each (object, function (key, value) {
if(key == "Model"){
taxMonth = object[key].taxMonth,
effectiveDate = object[key].effectiveDate
}
});
});
在那之后,我尝试推送所有像这样的事件
events.push({
"id" : formId,
"fornCode" : fornCode,
"taxMonth" : taxMonth,
"effectiveDate" : effectiveDate
});
callback(events);
},
但它只显示"id and fornCode",但"taxMonth effectiedDate"未定义那么,有什么我可以在事件中循环和推送它吗?想要这样改变
{formId : '9', fornCode : 'PP40', taxMonth : '0', effectiveDate : '2018-10-09'},
{formId : '9', fornCode : 'PP40', taxMonth : '3', effectiveDate : '2018-11-10'},
{formId : '9', fornCode : 'PP40', taxMonth : '10', effectiveDate : '2018-12-19'},
{formId : '18', fornCode : 'PP30', taxMonth : '0', effectiveDate : '2018-01-09'},
{formId : '18', fornCode : 'PP30', taxMonth : '3', effectiveDate : '2018-04-10'},
{formId : '18', fornCode : 'PP30', taxMonth : '10', effectiveDate : '2018-11-19'}
//我正在使用FullCalendar,所以我必须推送事件中的所有数据。我不确定这是不是一个好办法,如果有任何建议,请感谢我。非常感谢。^^
您必须从Model对象创建新的数组。并且不需要使用CCD_ 1。运行这个代码段,看看它是如何工作的。
var events = [];
var num_events = [
{"formId": 9,
"fornCode": "PP40",
"Model": [
{"taxMonth": 0,
"effectiveDate": "2018-10-09"
},
{
"taxMonth": 3,
"effectiveDate": "2018-11-10"
},
{
"taxMonth": 10,
"effectiveDate": "2018-12-19"
}
]
},{"formId": 18,
"fornCode": "PP30",
"Model": [
{"taxMonth": 0,
"effectiveDate": "2018-01-09"
},
{
"taxMonth": 3,
"effectiveDate": "2018-04-10"
},
{
"taxMonth": 10,
"effectiveDate": "2018-11-19"
}
]
}
];
$.each (num_events, function (index, object) {
formId = num_events[index].formId,
fornCode = num_events[index].fornCode,
fornShortTh = num_events[index].fornShortTh
taxMonth = []
effectiveDate = [];
$.each (object.Model, function (key, value) {
events.push({
"id" : formId,
"fornCode" : fornCode,
"taxMonth" : taxMonth,
"effectiveDate" : effectiveDate,
"taxMonth" : value.taxMonth,
"effectiveDate" : value.effectiveDate
});
});
});
console.log(events);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我不确定是否真的理解你想做什么,但可能是这样的:
$.each (object, function (key, value) {
if(key == "Model"){
$.each(value, (i, e) => {
events.push({
"id" : formId,
"fornCode" : fornCode,
"taxMonth" : e.taxMonth,
"effectiveDate" : e.effectiveDate
});
})
}
});