Die obigen Antworten sind gut. Aber das ist auch gut und nützlich.
!obj['your_key'] // if 'your_key' not in obj the result --> true
Es ist gut für kurze Codestile, insbesondere für if-Anweisungen:
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');
}
Hinweis: Wenn Ihr Schlüssel gleich null oder "" ist, ist Ihre "if" -Anweisung falsch.
obj = {'a': '', 'b': null, 'd': 'value'}
!obj['a'] // result ---> true
!obj['b'] // result ---> true
!obj['c'] // result ---> true
!obj['d'] // result ---> false
Der beste Weg, um zu überprüfen, ob ein Schlüssel in einem Objekt vorhanden ist, ist:'a' in obj