javascript 如果值存在于 JSON 中,则检查 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11422366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:09:48  来源:igfitidea点击:

Check in jQuery if Value exists in JSON

javascriptjqueryjson

提问by Yasir

[
    {
        "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"); });

回答by Oleg V. Volkov

Just gallery.imageand gallery.videowithout anything else in condition will fix it.

只要gallery.imagegallery.video没有任何其他条件将修复它。

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.

你的第一个问题是你使用单=赋值,而不是比较或者=====。这种首先(图像)检查总是成功,并且还会用 value 覆盖您存储在其中的图像链接true

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

其次,true除非您严格遵守===实际true价值,否则您实际上不需要与任何东西进行比较。只需在条件中单独使用任何真实值。

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

最后,你实际操作对象。一旦 jQuery 为您解码 JSON,这不再与 JSON 有任何关系。

回答by vishakvkt

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

回答by pb2q

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

检查属性是否存在的规范方法(为特定对象定义)是使用typeof运算符并检查字符串"undefined",使用身份比较运算符===!==

Like this:

像这样:

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

If imageis nota property of gallery, then typeof gallery.imagewill always be "undefined". Using one of the identity operators rather than the standard equality operators avoids problems due to silent type conversion.

如果image没有的属性gallery,那么typeof gallery.image将永远是"undefined"。使用恒等运算符之一而不是标准的相等运算符可以避免由于静默类型转换引起的问题。

Other approaches are subject to numerous inconsistencies/issues.

其他方法存在许多不一致/问题。

Your own code simply assigns a value to the imageproperty in your iftest.

您自己的代码只是为测试中的image属性分配一个值if