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?

Nathan Dawson
19.4k3 gold badges59 silver badges60 bronze badges
asked May 14, 2015 at 0:43
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.

answered May 14, 2015 at 0:51
2
  • 1
    in his json, calls is an array of arrays, getting the ith element makes sense Commented May 14, 2015 at 1:34
  • i is the index of item in data.calls Commented May 14, 2015 at 1:41
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 );
 }
 });
 });
answered May 14, 2015 at 1:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.