jQuery 检查 JS 对象中是否存在键

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

Checking if a key exists in a JS object

javascriptjquery

提问by user2065483

I have the following JavaScript object:

我有以下 JavaScript 对象:

var obj = {
    "key1" : val,
    "key2" : val,
    "key3" : val
}

Is there a way to check if a key exists in the array, similar to this?

有没有办法检查数组中是否存在一个键,类似于这个?

testArray = jQuery.inArray("key1", obj);

does not work.

不起作用。

Do I have to iterate through the obj like this?

我必须像这样遍历 obj 吗?

jQuery.each(obj, function(key,val)){}

回答by Sirko

Use the inoperator:

使用in运算符

testArray = 'key1' in obj;


Sidenote: What you got there, is actually no jQuery object, but just a plain JavaScript Object.

旁注:你得到的实际上不是 jQuery 对象,而是一个普通的 JavaScript 对象。

回答by Guffa

That's not a jQuery object, it's just an object.

那不是一个 jQuery 对象,它只是一个对象。

You can use the hasOwnProperty method to check for a key:

您可以使用 hasOwnProperty 方法来检查密钥:

if (obj.hasOwnProperty("key1")) {
  ...
}

回答by Fernando_Jr

var obj = {
    "key1" : "k1",
    "key2" : "k2",
    "key3" : "k3"
};

if ("key1" in obj)
    console.log("has key1 in obj");

=========================================================================

================================================== ========================

To access a child key of another key

访问另一个键的子键

var obj = {
    "key1": "k1",
    "key2": "k2",
    "key3": "k3",
    "key4": {
        "keyF": "kf"
    }
};

if ("keyF" in obj.key4)
    console.log("has keyF in obj");

回答by Ali Hallaji

Above answers are good. But this is good too and useful.

上面的答案很好。但这也很好,很有用。

!obj['your_key']  // if 'your_key' not in obj the result --> true

It's good for short style of code special in if statements:

它适用于 if 语句中的短代码风格:

if (!obj['your_key']){
    // if 'your_key' not exist in obj
    console.log('key not in obj');
} else {
    // if 'your_key' exist in obj
    console.log('key exist in obj');
}

Note: If your key be equal to null or "" your "if" statement will be wrong.

注意:如果您的键等于 null 或 "",您的 "if" 语句将是错误的。

obj = {'a': '', 'b': null, 'd': 'value'}
!obj['a']    // result ---> true
!obj['b']    // result ---> true
!obj['c']    // result ---> true
!obj['d']    // result ---> false

So, best way for checking if a key exists in a obj is:'a' in obj

因此,检查 obj 中是否存在键的最佳方法是:'a' in obj

回答by Diablo

map.has(key)is the latest ECMAScript 2015way of checking the existance of a key in a map. Refer to thisfor complete details.

map.has(key)ECMAScript 2015最新 的检查映射中键是否存在的方法。有关完整的详细信息,请参阅此内容。

回答by Khan

the simplest way is

最简单的方法是

const obj = {
  a: 'value of a',
  b: 'value of b',
  c: 'value of c'
};

if(obj.a){
  console.log(obj.a);
}else{
  console.log('obj.a does not exist');
}

回答by Achsuthan

You can try this:

你可以试试这个:

const data = {
  name : "Test",
  value: 12
}

if("name" in data){
  //Found
}
else {
  //Not found
}