What is the simplified version to use ||
for 2 or more times
This does not work.
`${e.ownerName || e.orgId.ownerName || ""}`
What is the simplified version to use ||
for 2 or more times
This does not work.
`${e.ownerName || e.orgId.ownerName || ""}`
I don't think this can be simplified any further due to the pigeon-hole principle. You will always need N - 1 operators (AND or OR) for N values. in your example 2 operators for 3 values.
besides, the compiler will automatically return the very first value that equal to true and stop the evaluation due to Short-Circuit evaluation. So it's optimized by default.
hmm .. that looks right..
keep in mind that the 'OR' operator will return the next value only if previous value is false, empty or null.
What values are you checking? i.e. what value do you get if you log to console (console.log(e.ownerName, e.orgId.ownerName
)?
Solved By this syntax
{e.orgId ? e.orgId.orgName : e.orgName || ""}
Looks like you were attempting to apply the "guard pattern" (actually uses logical AND (&&
) versus OR (||
)) or essentially doing null checks, and ultimately providing a fallback value in some cases.
If you want the values in this order of fallback
e.ownerName
e.orgId.ownerName
""
:You can use the guard (null-check) patten and fallback
e.ownerName || (e.orgId && e.orgId.ownerName) || ''
Or, if your build env supports it, use the Optional Chaining operator to simplify it (the null checks) a bit.
e.ownerName || e.orgId?.ownerName || ''