Search code examples
jqueryloopsfullcalendarnested-loops

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


I am new to jQuery and hope someone can help me with this. I have an JSON data which are as follows

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"
            }
        ]
    }
];

I try to loop all this data like this

$.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
        }
    });
});

After that i try to push all events like this

events.push({
    "id"                        : formId,
    "fornCode"                  : fornCode,

    "taxMonth"                  : taxMonth,
    "effectiveDate"             : effectiveDate
});

callback(events);
},

but it only show "id and fornCode " but "taxMonth effectiveDate" is undefined So is there any that i can loop and push it in events? want to change like this

{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'}

//I am working with FullCalendar so i have to push all data in events. I am not sure that is this good way to this, If any suggestion will be thankful to me. Many thanks in advance. ^^


Solution

  • You have to create new arrays from the Model object. And no need to use if(key == "Model"). Run the snippet and see how it works.

    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>