1

I am a little confused with checking if an array value us empty from a json response.

{
  "status": "success",
  "message": "All pages re-ordered",
  "content": {
    "_wysihtml5_mode": "1",
    "Page": {
        "title": "cover",
        "page_text": "dfvdfvdfvdfvdv story go daddy xxx",
        "storyborad_img": "1jhkjh.png",
        "background_url": "kjbj.png",
        "newBackground_url": "",
        "text_font": "arial",
        "id": "30",
        "book_id": "38",
        "newStoryborad_img": {
            "name": "1jhkjh.png",
            "type": "image\/png",
            "tmp_name": "\/Applications\/MAMP\/tmp\/php\/phpvyf8Xx",
            "error": 0,
            "size": 185607
        }
    },
    "User": {
        "username": "testuser"
    }
  }
}

I have tried to check typeof array == undefined and .length but both give me what is in the else statement newBackground_url

var page = $.parseJSON(xhr.responseText.replace('</p>', ''));
var imageType;
if(page.content.Page.newStoryborad_img.length > 0) {
    imageType = page.content.Page.newStoryborad_img.name;
}
else {
    imageType = page.content.Page.newBackground_url.name;
}
6
  • Why is there a </p> in your JSON? The path should be page.content.Page.storyborad_img [sic] according to your code. Commented Jun 17, 2014 at 21:48
  • </p> was part of the response, but I am changing it around so will remove that once I have the code refactored. Commented Jun 17, 2014 at 21:51
  • 'Storyborad' reminded me of kazakhstana.files.wordpress.com/2011/09/borat-swimsuit.jpg Commented Jun 17, 2014 at 21:51
  • 3
    Isn't newStoryborad_img an object? Or, do you receive an array when there are more than one images? Commented Jun 17, 2014 at 21:55
  • 1
    typeof(array) == undefined should give you the else statement, because you have defined it in your json. You might want typeof(array) != 'undefined' if you want its presence to trigger the if block. (which is what it looks like you want it to do) Commented Jun 17, 2014 at 21:59

1 Answer 1

2

It depends. According to your example, you are returning an object "newStoryborad_img": {...}

In which case you would want to use typeof(page.content.Page.newStoryborad_img) != 'undefined'

Demo typeof: http://jsfiddle.net/gunderjt/cn2cs/

But if you are looking at having an array of objects "newStoryborad_img": [{...}] (notice the brackets). Then to check the presence of any objects in the array (assuming you are returning an empty array)

page.content.Page.newStoryborad_img.length > 0 will be what you want

Demo length: http://jsfiddle.net/gunderjt/cn2cs/1/

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

2 Comments

thanks for the help. If I change "newStoryborad_img": "" I get if instead of else for the typeof. I will always be passing newStoryborad_img either empty or with values
@KeithPower, and this is an update to @JTG's answer/fiddle, change the if statement condition to typeof(page.content.Page.newStoryborad_img) != 'string' meaning it's an object or anything else that is not a string, then console.log if. If it's a string i.e. "", then console.log else. JS Fiddle

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.