Reputation: 123
I have an empty array like this:
{"18":[]}
but other JSON are like this:
{
"223": [
{
"virtuemart_state_id": "1",
"state_name": "Alabama"
},
{
"virtuemart_state_id": "2",
"state_name": "Alaska"
}
]
}
I was trying to check that like this:
jQuery("#paramsstates_chzn .search-field: input").click(function(){
var country = jQuery("#country").val(),
chzn,
url = "http://localhost/data.php?id="+country;
jQuery.getJSON(url, function(data){
console.log(data);
if (jQuery.isEmptyObject(data[country])) {
chzn = "<li class='active-result'>Empty</li>";
jQuery("ul.chzn-results").html(chzn);
return;
}
$.each(data[country], function(i, rep){
chzn += "<li class='active-result' id ='paramsstates_chzn_o_"+ i + "' >"+ rep.state_name +"</li>";
});
jQuery("ul.chzn-results").html(chzn);
});
});
Any idea in where I made mistake because it's wasn't updating ul.chzn-results class area.
HTML:
<ul class="chzn-results"></ul>
I tried like this too for length check. But not working.
(data[country].length === 0)
Upvotes: 1
Views: 222
Reputation: 16498
You can use use : (data[country]).length ==0
to check if array is empty
jQuery("#paramsstates_chzn .search-field: input").click(function(){
var country = jQuery("#country").val(),
chzn,
url = "http://localhost/joomla/data.json";
jQuery.getJSON(url, function(data){
console.log(data);
if ((data[country]).length ==0) {
chzn = "<li class='active-result'>Empty</li>";
jQuery("ul.chzn-results").html(chzn);
return;
}
$.each(data[country], function(i, rep){
chzn += "<li class='active-result' id ='paramsstates_chzn_o_"+ i + "' >"+ rep.state_name +"</li>";
});
jQuery("ul.chzn-results").html(chzn);
});
});
Upvotes: 0
Reputation: 13734
Change
if (jQuery.isEmptyObject(data[country])) {
to
if (data[country].length==0) {
Upvotes: 0
Reputation:
No need for jQuery, that is plain JavaScript.
var o = {"18":[]};
var isEmpty = o["18"].length === 0;
Upvotes: 1