Javascript 如何检查密钥是否存在于 json 中

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

How to check key exist in json or not

javascriptjqueryjson

提问by User97798

I have JSON object and i want to check key is set in that JSON object

我有 JSON 对象,我想检查该 JSON 对象中是否设置了密钥

Here is JSON object

这是 JSON 对象

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18",
            "Child": [{
                "FromAge": "0",
                "ToAge": "12",
                "Price": "10"
            }]
        }
    }
}

If JSON Object like this as you can see Childis not exist, then how to check this

如果像你看到的Child这样的 JSON 对象不存在,那么如何检查这个

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18"
        }
    }
}

I have tried

我试过了

if(Data_Array.Private.Price.Child[0].Price != "undefined"){
    ...
}

But it is showing me this error

但它向我展示了这个错误

Uncaught TypeError: Cannot read property

未捕获的类型错误:无法读取属性

I am not be able to find out what should i do.

我不知道该怎么办。

回答by

var json = {key1: 'value1', key2: 'value2'}

"key1" in json ? console.log('key exists') : console.log('unknown key')

"key3" in json ? console.log('key exists') : console.log('unknown key')

for child key

用于儿童钥匙

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18",
            "Child": [{
                "FromAge": "0",
                "ToAge": "12",
                "Price": "10"
            }]
        }
    }
}

'Child' in Data_Array.Private.Price ? console.log('Child detected') : console.log('Child missing')

create variable child

创建变量孩子

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18",
            "Child": [{
                "FromAge": "0",
                "ToAge": "12",
                "Price": "10"
            }]
        }
    }
}

var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'

console.log(child)

if there is no child

如果没有孩子

var Data_Array = {
    "Private": {
        "Price": {
            "Adult": "18"
        }
    }
}

var child = 'Child' in Data_Array.Private.Price && Data_Array.Private.Price.Child[0] || 'there is no child'

console.log(child)

回答by Lucky Soni

Trying to get a property off of an object that is not defined will raise an exception. You need to check each property for existence (and type) throughout the chain (unless you are sure of the structure).

尝试从未定义的对象中获取属性将引发异常。您需要检查整个链中的每个属性是否存在(和类型)(除非您确定结构)。

Using Lodash:

使用 Lodash:

if(_.has(Data_Array, 'Private.Price.Child')) {
    if(Array.isArray(Data_Array.Private.Price.Child) && Data_Array.Private.Price.Child.length && Data_Array.Private.Price.Child[0].Price) {
        // Its has a price!
    }
}

回答by Nina Scholz

You could use the inoperatorfor an object.

您可以将in运算符用于对象。

The inoperator returns trueif the specified property is in the specified object.

in运营商的回报true,如果指定的属性是在指定的对象。

if ('Child' in Data_Array.Private.Price) {
    // more code
}

回答by rm4

Check if Child is undefined

检查 Child 是否未定义

if (typeof Data_Array.Private.Price.Child!== "undefined") {
    ...
}

Or you can use in:

或者你可以使用in

if ("Child" in Data_Array.Private.Price) {
    ...
}

Or you can use _.isUndefined(Data_Array.Private.Price.Child)of underscore.js

或者你可以使用_.isUndefined(Data_Array.Private.Price.Child)underscore.js