forjs check if key in json 1

forjs check if key in json

var json = {key1: 'value1', key2: 'value2'}

"key1" in json ? console.log('key exists') : console.log('unknown key')

"key3" in json ? console.log('key exists') : console.log('unknown key')

Here is what the above code is Doing:
1. We create a JSON object with two keys.
2. We check if the key1 exists in the JSON object. If it does, we print key exists. If it doesn’t, we print unknown key.
3. We check if the key3 exists in the JSON object. If it does, we print key exists. If it doesn’t, we print unknown key.

Similar Posts