如何在 jquery 和 event.push() 中嵌套循环

How to nested loop in jquery and event.push()

我是 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 effectiveDate" 未定义 那么有什么我可以循环并将其推送到事件中的吗? 想这样改

{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,所以我必须将所有数据推送到事件中。 我不确定这是解决此问题的好方法,如果有任何建议,我将不胜感激。 提前谢谢了。 ^^

我不确定是否真的理解你想做什么,但也许是这样的:

$.each (object, function (key, value) {
    if(key == "Model"){
        $.each(value, (i, e) => {
            events.push({
                "id" : formId,
                "fornCode" : fornCode,
                "taxMonth" : e.taxMonth,
                "effectiveDate" : e.effectiveDate
             });
        })
    }
});

您必须从模型对象创建新数组。而且不需要使用 if(key == "Model")。 运行 代码片段,看看它是如何工作的。

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>