1

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?

1
  • your code works for me in my fiddle, unless your actual json is not the one you posted jsfiddle.net/etuLsedk Commented May 14, 2015 at 1:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

in his json, calls is an array of arrays, getting the ith element makes sense
i is the index of item in data.calls
0

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

            });
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.