In ruby, there is a beautiful method that is called .try
that allows to access object attributes/methods without risking to get an error.
Eg.
{hello: 'world'}.try(:[], :hello) # 'world'
nil.try(:[], :hello) # nil (no error)
Is there a way to achieve this syntax in ES6 elegantly?
For now, I keep writing the ugly:
if (object && object.key && object.key.subkey) {
return object.key.subkey
}
Thanks