javascript - How to loop through a JSON array using jQuery -
i trying loop through json array.
here 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 jquery code.
<script> $(function(){ function isset(a, b){ if(typeof !== "undefined" && a){ return } 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 ' + eic_remoteaddress + ' mid: ' + accordi_mid ); } if(eic_calldirection == 'o' && eic_state == 'c'){ console.log('live call ' + eic_remoteaddress ); } }); }); }, 1000); }); </script>
my code working since runs every second keep getting error in logs
typeerror: c undefined
and error point line
console.log(c);
what cause of issue , how can correct it?
in function:
$.each(data.calls, function(i, item){});
item
value of each element in data.calls
, don't need use item[i]
. c = item
enough.
Comments
Post a Comment