Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  140] [ 2]  / answers: 1 / hits: 34093  / 8 Years ago, mon, june 27, 2016, 12:00:00

I have the following object literal:



{ 
'key1':
{
id: 'rr323',
d: undefined,
x: 560,
y: 150
},
'key2':
{
id: 'rr231',
d: undefined,
x: 860,
y: 90
}
}


I want to implement an if statement such as below:



if(key DOES NOT exist in object){  
//perform certain function
}


I tried the following:



var key = key1;
if(!(key in global_move_obj)){
// function
}


But that always returns true value when it should return false.


More From » object

 Answers
20

Use the hasOwnProperty call:


if (!obj.hasOwnProperty(key)) {

}

[#61612] Saturday, June 25, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brockg

Total Points: 55
Total Questions: 104
Total Answers: 104

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;