0

I have a little script which grabs some JSON content for a webshop menu. Sometimes a category doesn't have products in it so when the array is empty a "no products found" line must be showed. I tried this with jQuery.isEmptyObject() but now the text is only showed at categories WITH products in it. The menu itself works perfectly except for the thing above...

So my question: How can I check if json.products or product is empty? If so how would you incorporate that and the end of the script?

Probably an easy one for you.... I don't see it anymore ;)

What I have:

function widget(catId, catHref){   
  var url = catHref + 'page1.ajax?limit=4';
  $.getJSON(url, function (json){     
    var productsHtml = [];

      $.each(json.products, function(index, product){

        var productHtml = '' +
          ..... blablabla .....

        productsHtml.push(productHtml);
      });
      productsHtml = productsHtml.join('');


    if (jQuery.isEmptyObject(json[productsHtml])) {
      $('#widget-products'+catId+' ul').html('No products');
    } else{
      $('#widget-products'+catId+' ul').html(productsHtml);
    }

  });
}

2 Answers 2

1

You are converting an array to a string with this line:

productsHtml = productsHtml.join('');

So, just do this:

if ('' == productsHtml) {
    // blah blah

Edit based on comments and edits to question:

To check if json.products is empty, just check json.products.length:

if (!json.products.length) { // implies json.products is empty

You don't specify what product is and you never use it in the code above. If it's an array, check product.length. For a string, compare to ''. If it's an object, then use jQuery.isEmptyObject(product).

Edit 2 based on comments:

To check if the JSON object has a given property, you use .hasOwnProperty(), like this:

if(!json.hasOwnProperty('collection') || !json.collection.hasOwnProperty('products')) {

If there is no collection property, this short-circuits, and you know you don't have collection.products. If it has a collection property, it keeps going and checks for the collection.products property.

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

3 Comments

Well then is my question completely wrong! Can you have another look please? I've updated the question... thx!
I think again my question is wrong. Could you please take a look here -> jsfiddle.net/meules/AQzXL/1. A json file with products in it has 'collection.products' in it. A json file without products in it don't. I tried to explain it with some notes inside the code. If you could help I would be more then tahnkfull ;)
Ah, now I see what you are trying to do. Please see my second edit above; that should take care of your problem.
1

Just check the length of the array like this.

if(productsHtml.length>0)
{
//do your stuff here...
}

for new question i have updated your code and added some comments to make you understand:-

$.getJSON(url, function(json) {
    var productsHtml = [];
    //here i am checking json or json.products is not undefined
    if (json && json.products) {
        $.each(json.products, function(index, product) {
            //checking if product is not undefined
            if (product) {

                //do your stuff
                var productHtml = '' +
                    .....blablabla.....

                productsHtml.push(productHtml);
            }
        });
        productsHtml = productsHtml.join('');


        if (jQuery.isEmptyObject(json[productsHtml])) {
            $('#widget-products' + catId + ' ul').html('No products');
        } else {
            $('#widget-products' + catId + ' ul').html(productsHtml);
        }
    }

});

3 Comments

Well then is my question completely wrong! Can you have another look please? I've updated the question... thx!
Thx for your update. The weird part is that 'no products' is shown when there are actually products. Any thoughts on that? I'm completly stuck on this one.
I think again my question is wrong. Could you please take a look here -> jsfiddle.net/meules/AQzXL/1. A json file with products in it has 'collection.products' in it. A json file without products in it don't. I tried to explain it with some notes inside the code. If you could help I would be more then tahnkfull ;)

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.