Javascript 使用 jQuery 检查 JSON 对象中是否存在密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8893020/
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-08-24 07:57:33  来源:igfitidea点击:

Check if key exists in JSON object using jQuery

javascriptjqueryjson

提问by nidhin

I have done AJAX validation and validated message is returned as a JSON array. Therefore I need to check whether the keys, like nameand email, are in that JSON array.

我已经完成了 AJAX 验证,并且经过验证的消息作为 JSON 数组返回。因此,我需要检查键(如nameemail)是否在该 JSON 数组中。

{
    "name": {
        "isEmpty": "Value is required and can't be empty"
    },
    "email": {
        "isEmpty": "Value is required and can't be empty"
    }
}

Only if the key name is present, I need to write an error message to the namefield.

仅当键名存在时,我才需要向该name字段写入错误消息。

Following is the code to display an error if fields is entered

以下是在输入字段时显示错误的代码

if (obj['name']'isEmpty'] != "") {                                 
   $('#name').after(c1 + "<label class='error'>" + obj['name']['isEmpty'] + "</label>");
}                                       
if (obj['email']['isEmpty'] != "" ) { 
   $('#email').after(c4 + "<label class='error'>" + obj['email']['isEmpty'] + "</label>");
}

But if the namefield is entered, it will not be in JSON array. So the checking statement

但是如果name输入了该字段,它就不会在 JSON 数组中。所以检查语句

if (obj['name']['isEmpty'] != "")

will result in the following error:

将导致以下错误:

obj.name not found

找不到对象名称


It is not necessary to have key namein the array. At same time I need to check for this to display the error if the array possesses the key name.


没有必要name在数组中有键。同时,如果数组拥有 key ,我需要检查这个以显示错误name

回答by Dau

Use JavaScript's hasOwnProperty()function:

使用 JavaScript 的hasOwnProperty()函数:

if (json_object.hasOwnProperty('name')) {
    //do struff
}

回答by Pokuri

No need of JQuery simply you can do

不需要 JQuery 就可以了

if(yourObject['email']){
 // what if this property exists.
}

as with any value for emailwill return you true, if there is no such property or that property value is nullor undefinedwill result to false

与任何值email将返回true,如果没有这样的属性或属性值nullundefined将导致以false

回答by jujiyangasli

if(typeof theObject['key'] != 'undefined'){
     //key exists, do stuff
}

//or

if(typeof theObject.key != 'undefined'){
    //object exists, do stuff
}

I'm writing here because no one seems to give the right answer..

我写在这里是因为似乎没有人给出正确的答案..

I know it's old...

我知道它很旧...

Somebody might question the same thing..

有人可能会质疑同样的事情..

回答by Vahap Gencdal

if you have an array

如果你有一个数组

var subcategories=[{name:"test",desc:"test"}];

function hasCategory(nameStr) {
        for(let i=0;i<subcategories.length;i++){
            if(subcategories[i].name===nameStr){
                return true;
            }
        }
        return false;
    }

if you have an object

如果你有一个对象

var category={name:"asd",test:""};

if(category.hasOwnProperty('name')){//or category.name!==undefined
   return true;
}else{
   return false;
}