简体   繁体   中英

How to loop through a JSON array using jQuery

I am trying to loop through a JSON array.

here is example output

{
    "calls": [
        [
            {
                "interactionId": "2002766591",
                "account_id": "",
                "mid": "",
                "Eic_CallDirection": "O",
                "Eic_RemoteAddress": "5462223378",
                "Eic_LocalAddress": "1062",
                "Eic_State": "I"
            }
        ]
    ],
    "status": [
        {
            "statusId": "Available",
            "userId": "su",
            "loggedIn": false
        }
    ]
}

here is my jQuery code.

<script>
$(function(){

    function isset(a, b){

        if(typeof a !== "undefined" && a){
            return a
        }

        return b;
    }

    setInterval(function() {

        $.getJSON("showMEssages.php", {method: "getMessages"}, function(data){

            if(data.calls.length == 0 || !data.calls){
                console.log('No Messages');
            }
            var c;

            $.each(data.calls, function(i, item){

                c = item[i];
                console.log(c);

                var interactionId = isset(c.interactionId, 0);
                var Eic_CallDirection = isset(c.Eic_CallDirection, '');
                var Eic_State = isset(c.Eic_State, '');
                var AccoRDI_mid = isset(c.AccoRDI_mid, '');
                var Eic_RemoteAddress = isset(c.Eic_RemoteAddress, '');

                if( Eic_CallDirection == 'I' && Eic_State == 'A'){
                    console.log('Call From ' + Eic_RemoteAddress + ' MID: ' + AccoRDI_mid );
                }

                if(Eic_CallDirection == 'O' && Eic_State == 'C'){
                    console.log('Live Call With ' + Eic_RemoteAddress );
                }


            });
        });

    }, 1000);
});
</script>

my code is working but I get since it runs every second I keep getting this error in the logs

TypeError: c is undefined

and the error point to this line

console.log(c);

what is the cause of this issue and how can I correct it?

In this function:

$.each(data.calls, function(i, item){});

item is the value of each element in data.calls , so you don't need to use item[i] . Just c = item is enough.

I figured out the issue. I had to do a loop with in a loop since I have array with in array

        $.each(data.calls, function(i, item){

            $.each(item, function(z, c){

                var interactionId = isset(c.interactionId, 0);
                var Eic_CallDirection = isset(c.Eic_CallDirection, '');
                var Eic_State = isset(c.Eic_State, '');
                var AccoRDI_mid = isset(c.AccoRDI_mid, '');
                var Eic_RemoteAddress = isset(c.Eic_RemoteAddress, '');

                if( Eic_CallDirection == 'I' && Eic_State == 'A'){
                    console.log('Call From ' + Eic_RemoteAddress + ' MID: ' + AccoRDI_mid );
                }

                if(Eic_CallDirection == 'O' && Eic_State == 'C'){
                    console.log('Live Call With ' + Eic_RemoteAddress );
                }

            });
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM