I am working on Angular project and time to time I used to have check undefined
or null
over Object or it's properties. Normally I use lodash _.isUndefined()
see example below:
this.selectedItem.filter(i => {
if(_.isUndefined(i.id)) {
this.selectedItem.pop();
}
})
I couldn't see any problem with it. But I had discussion with my colleague during review of above code. He was telling me that if i
gets undefined
before the if
statement then it will throw the exception. Instead he suggested me to always check i
or i.id
like this:
if(!!i && !!i.id) {
this.selectedItem.pop();
}
I am convinced what he was trying to say unlike his way of checking undefined
in above code. But then I was thinking what is the purpose of lodash _.isUndefined
?
Could anyone please let me know what is the best or clean way to do it. Because for me !!i && !!i.id
is not readable at all.
Many thanks in advance.