I only found solutions for variables, that were undefined, not the use case that I have at the moment.
I am building an object, from another object, where I am assigning a number of values from the input object, e.g.
valueQuantity: {
value: observation['obx.5'][0]['obx.5.1'][0],
unit: observation['obx.6'][0]['obx.6.1'][0]
},
In some input object the values are defined in some they aren't. In that case I want value and unit to be null, however this errors out obviously.
(node:36779) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
In this case observation['obx.5'][0] was already undefined.
I could do a try/catch, but as the resulting object is pretty long, this would become extremely unreadable.
Any way to do this gracefully?