admin管理员组文章数量:1133922
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);
does not work.
Do I have to iterate through the obj like this?
jQuery.each(obj, function(key,val)){}
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);
does not work.
Do I have to iterate through the obj like this?
jQuery.each(obj, function(key,val)){}
Share
Improve this question
edited Jun 23, 2015 at 19:37
Sumner Evans
9,1475 gold badges31 silver badges48 bronze badges
asked Jun 15, 2013 at 18:09
user2065483user2065483
1,2253 gold badges9 silver badges12 bronze badges
6
|
Show 1 more comment
10 Answers
Reset to default 205Use the in
operator:
testArray = 'key1' in obj;
Sidenote: What you got there, is actually no jQuery object, but just a plain JavaScript Object.
That's not a jQuery object, it's just an object.
You can use the hasOwnProperty method to check for a key:
if (obj.hasOwnProperty("key1")) {
...
}
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");
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 (!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.
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
use the hasOwnProperty()
,
if (!obj.hasOwnProperty(key)) {
}
For Example :
const object1 = {
one : 'value of one',
two : 'value of two',
three : 'value of three',
};
console.log(object1.hasOwnProperty('one'));
// expected output: true
console.log(object1.hasOwnProperty('value of one'));
// expected output: false
console.log(object1.hasOwnProperty('four'));
// expected output: false
You can try this:
const data = {
name : "Test",
value: 12
}
if("name" in data){
//Found
}
else {
//Not found
}
map.has(key)
is the latest ECMAScript 2015
way of checking the existance of a key in a map. Refer to this for complete details.
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');
}
You can use Object.keys(object).indexOf()
scenario:
- With
Object.keys(object)
you create an array of keys. - Use
indexOf()
to find out if the key you're looking for is part of this array:
let testObject = {
a: 1,
b: 2
}
Object.keys(testObject).indexOf('a') > -1 // expect true - the key exists
Object.keys(testObject).indexOf('c') > -1 // expect false - the key didn't exists
This works for me like a charm. I'm inside a foreach
function
this didn't work obj.hasOwnProperty("key1")
also this "key1" in obj
let $schedule = {lesson:'asd',age:'sad'}
$schedules.forEach(function(e) {
if (e['lesson']) {
$title = e.lesson.lesson_name;
} else {
$title = 'No lesson Attached';
}
});
本文标签: javascriptChecking if a key exists in a JS objectStack Overflow
版权声明:本文标题:javascript - Checking if a key exists in a JS object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736791965a1953128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
obj.has("key1")
will work if you are using ES6. – Diablo Commented May 30, 2017 at 9:03obj.hasOwnProperty("key1")
instead of justhas
? – Aminu Kano Commented Aug 21, 2018 at 9:11