I'm using Array.find in one of my JavaScript handlers, and discovered that sometimes there is no truthy test, and so the property gets set to undefined. I worked around it like this:
const previous = this.value;
this.value = this.values.find((value) => value.Id === event.detail.valueId);
if (!this.value) {
this.value = previous;
}
Is there any way to give a default value to the find method so that if nothing is found it uses that as a fallback?
arr.find() || fallbackValueor with newer syntaxarr.find() ?? fallbackValue.this.value. Are they all guaranteed to be objects? Then.find(...) || previousis fine. If they might have falsey values, then use multiple lines as you have but be specific about testing against undefined.??is specific for null and undefined only, but there's still a little room for a false positive ifnullis a legal value.