I have the following JavaScript object:
JavaScript
x
6
1
var obj = {
2
"key1" : val,
3
"key2" : val,
4
"key3" : val
5
}
6
Is there a way to check if a key exists in the array, similar to this?
JavaScript
1
2
1
testArray = jQuery.inArray("key1", obj);
2
does not work.
Do I have to iterate through the obj like this?
JavaScript
1
2
1
jQuery.each(obj, function(key,val)){}
2
Advertisement
Answer
Use the in
operator:
JavaScript
1
2
1
testArray = 'key1' in obj;
2
Sidenote: What you got there, is actually no jQuery object, but just a plain JavaScript Object.