1

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)
9
  • 1
    What do you mean by 'check'? Commented Jul 19, 2014 at 7:51
  • missing " for url = "localhost/joomla/data.json; .. correct it Commented Jul 19, 2014 at 7:52
  • 1
    Use if(Array.isArray(obj) === true && obj.length === 0) to check if an object is an array and is empty. Commented Jul 19, 2014 at 7:53
  • that's an empty array not logically an empty object, simply check for length. Commented Jul 19, 2014 at 7:54
  • I tried like this too : (data[country].length === 0) but no luck :( Commented Jul 19, 2014 at 7:58

3 Answers 3

1

No need for jQuery, that is plain JavaScript.

var o = {"18":[]};
var isEmpty = o["18"].length === 0;
Sign up to request clarification or add additional context in comments.

2 Comments

Did you read the other line of the code ? $.each(data[country], function(i, rep){ chzn += "<li class='active-result' id ='paramsstates_chzn_o_"+ i + "' >"+ rep.state_name +"</li>"; }); I don't have all JSON empty. I just want to check the empty one.
Your question only includes a very small snippet of JSON. Please include the real JSON in your question.
0

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

          });

3 Comments

Thanks for your reply. But I don't know why this wasn't updating this area <ul class="chzn-results"></ul> :(
please see here plnkr.co/edit/8bmZSolmCMlReir850Bf?p=preview erything looks fine double check your html code
in line 17 you can change country beteween 18 i 17 to see result
0

Change

if (jQuery.isEmptyObject(data[country])) {

to

if (data[country].length==0) {

2 Comments

@user3850712 Show us the real JSON.
does it add to ul, if the array is non-empty ?

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.