I have a for
-of
loop which goes like this:
for(const val of someArray[0].properties) {
// some processing;
}
Now for some reason if someArray[0].properties
is undefined, the loop breaks, saying:
Cannot read property 'Symbol(Symbol.iterator)' of undefined
If I try to use the !!
shorthand for boolean operators:
for (const val of !!someArray[0].properties && someArray[0].properties) {
}
it fails again.
The only solution I could come up with was:
if(someArray[0].properties){ // this will check for that undefined issue
for(const val of someArray[0].properties) {
// some processing;
}
}
Is there a more concise solution than this?
This is more concise:
for (const val of someArray[0].properties || []) {
// some processing
}
Basically, if someArray[0].properties
is undefined, the empty array is used instead of it throwing an error.