简体   繁体   中英

Check in jQuery if Value exists in JSON

[
    {
        "link"  : "images/examples/image-3.png", 
        "image" : "images/examples/image-3.png", 
        "title" : "copy"
    },
    {
        "link"  : "images/examples/image-3.png", 
        "video" : "video placeholder", 
        "title" : "copy"
    }               
]

In this example, I want to have a condition that says if this value is "video" do this else if value is "image" do this... but I seem to not be able to get handle of "video" or "image".

This is my function:

$.getJSON("javascripts/media.json", function(data) {                            
    $("<ul class='gallery'></ul>").prependTo("#content");

    for (var i=0; i<data.length; i++) {
        var gallery = data[i];

        //alert(data[1]);
        if (gallery.image = true) {
            $(
            "<li class='image'>" +
                "<a class=\"imageLink\" href='" + gallery.link + "' rel=\"lightbox[gallery]\">" + 
                    "<img src=" + gallery.image + " alt=" + gallery.title + ">" +
                "</a>"
            + "</li>").appendTo(".gallery");    
        }
        else if (gallery.video = true) {
            $(
            "<li class='video'>" +
                "<a class=\"videoLink\" href='" + gallery.link + "' rel=\"lightbox[gallery]\">" + 
                    "<img src=" + gallery.video + " alt=" + gallery.title + ">" +
                "</a>"
            + "</li>").appendTo(".gallery");    
        }

    }
}).error(function() { alert("error"); });

Just gallery.image and gallery.video without anything else in condition will fix it.

Your first problem is that you use single = - assignment, not comparison that would be either == or === . This making first (image) check always succeed and also overwrites image link you store in it with value true .

And second, you don't really need to compare anything with true unless you do strict === with real true value. Just use any truthy value in condition by itself.

And last, you actually operate on object. As soon as jQuery decoded JSON for you, this no longer have anything to do with JSON.

 if(gallery.image) {
 //do stuff with it
 } else if( gallery.video ) {
 //do stuff with video
 }

The canonical way of checking if a property exists - is defined for a particular object - is using typeof operator and checking against the string "undefined" , using the identity comparison operators === or !== .

Like this:

if (typeof gallery.image !== "undefined")
   // image code
else if (typeof gallery.video !== "undefined")
   // video code

If image is not a property of gallery , then typeof gallery.image will always be "undefined" . Using one of the identity operators rather than the standard equality operators avoids problems due to silent type conversion.

Other approaches are subject to numerous inconsistencies/issues.

Your own code simply assigns a value to the image property in your if test.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM