-5

What is the simplified version to use || for 2 or more times

This does not work.

`${e.ownerName || e.orgId.ownerName || ""}`
yudhiesh
  • 6,383
  • 3
  • 16
  • 49
Ayush Agarwal
  • 295
  • 2
  • 16
  • 1
    can you share the error or what happens when you run this code and what is the structure of the e object – Vikum Dheemantha Jan 03 '21 at 07:40
  • @Vikum Dheemantha `TypeError: Cannot read property 'orgName' of undefined` – Ayush Agarwal Jan 03 '21 at 07:45
  • 1
    The code you provided doesn't try to access a property called `orgName`, so that error doesn't relate to the code you provided. Please show the relevant code – Nick Parsons Jan 03 '21 at 07:46
  • it seems you are misunderstanding the error since there is no `orgName` in your code recheck your code and fid the place you are calling `orgName`. other than that there is no error in your code regarding OR operations. – Vikum Dheemantha Jan 03 '21 at 07:48
  • Yes, the variable is not available so, it must return null and check OR right ? – Ayush Agarwal Jan 03 '21 at 07:48
  • I think your error comes from somewhere else my friend. – zergski Jan 03 '21 at 07:51
  • @AyushAgarwal so your isue is e is undefined check that `orgDetails` object structure by console.log or some other method and verify it is json array or not. If it is array your forEach calling is valid then you have to check the object inside the array. – Vikum Dheemantha Jan 03 '21 at 07:52
  • Solved By {e.orgName || e.orgId ? e.orgId.orgName : ""} – Ayush Agarwal Jan 03 '21 at 07:58

4 Answers4

1

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.

Abobker Elaghel
  • 326
  • 2
  • 9
0

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)?

zergski
  • 793
  • 3
  • 10
  • `TypeError: Cannot read property 'orgName' of undefined` @zergski `${e.ownerName || e.orgId.ownerName}` it works fine but if both not found it fails – Ayush Agarwal Jan 03 '21 at 07:42
  • 'orgName'? looks like syntax error? are you sure the error is from those lines? – zergski Jan 03 '21 at 07:49
0

Solved By this syntax

{e.orgId ? e.orgId.orgName : e.orgName || ""}
Ayush Agarwal
  • 295
  • 2
  • 16
  • heh.. well.. if you still only want to use the 'OR' operator for better syntax you can just skip the orgId condition and do `||` on the rest.. – zergski Jan 03 '21 at 09:07
0

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

  1. e.ownerName
  2. e.orgId.ownerName
  3. "":

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 || ''
Drew Reese
  • 165,259
  • 14
  • 153
  • 181