本文介绍了检查JS对象中是否存在密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JavaScript对象:
I have the following JavaScript object:
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);
不起作用.
我必须像这样遍历obj吗?
Do I have to iterate through the obj like this?
jQuery.each(obj, function(key,val)){}
推荐答案
使用 in
运算符:
testArray = 'key1' in obj;
旁注:到那里,实际上没有jQuery对象,而只是一个普通的JavaScript对象.
Sidenote: What you got there, is actually no jQuery object, but just a plain JavaScript Object.
这篇关于检查JS对象中是否存在密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!