I am looking to extract the value from an array within an object while also implementing error checking. The code I currently have checks if a specific key exists in the object and if the value associated with that key is of type array. If both conditions are met, I want to retrieve the value from that key. While my current approach seems functional, I am wondering if there is a more efficient way to achieve this. I attempted using const [value] = obj?.the_key
, but received an exception
Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
when the value associated with 'theKey' is not an array or if 'theKey' does not exist within the object.
const obj = {'theKey': ['correct value']}
const hasKey = obj['theKey'] !== undefined && Array.isArray(obj.theKey)
if (!hasKey) console.log('null')
const [value] = obj.theKey
console.log(value)