I am reusing a function in my javascript for two places.
Within this method I make a call and retrieve a json object (data)
data[0].fields['status']
In one of the cases I know that the key ‘status’ won’t be there and I need to use a hardcoded value, but since the code is shared for both cases, I need to capture this case.
Is there a get method to check safely for the key and if it doesn’t exist I would use the hardcoded value?
data[0].fields.get('status') ?
Or is there is a better way?
Use the
inoperator to check whether a property is existent on an object:You also could use a simple boolean evaluation if your potential value is truthy, because the
undefinedevaluates to false. You can use that even in a short-circuit evaluation: