How do I set a default value to a variable if array.find returns 'undefined'.
Here's the line that's causing me issues. In some instances this variable will populate but in others, it won't and in that case, I want it to default to 0.
this.statistics.creditAmount = response.data.find(t => t.paymentType == 'RF' && t.status == 1).amount || 0;
(response.data.find(t => t.paymentType == 'RF' && t.status == 1) || { amount : 0 }).amount. But an if-else or a ternary operator check is much more readable IMO(data.find(t => t.paymentType == 'RF' && t.status == 1) || { amount : 0 })block first checks if there is value in the found object. If it isundefined, the OR operator will use the second object{ amount : 0 }. Then get theamountproperty from the object returned.