0

Trying to use nested ternary operator in my code

Code:

let ele_Partition = records[0].data.meeting ?  
  records[0].data.meeting.partition : records[0].data.partition;

Need to add another data for the same condition.

records[0].data.meeting.meetingPartition

How to implement it without nested if-else?

R9102
  • 687
  • 1
  • 9
  • 32
  • Just wrap your inner ternary expression in parentheses and put it in the place of one of your variables. –  Jan 15 '19 at 12:45
  • What do you mean by "add another data"? How would you want to combine the data? – Bergi Jan 15 '19 at 12:45
  • 2
    What did you try with `if`/`else`, can you please show us that attempt? – Bergi Jan 15 '19 at 12:46
  • `How to use nested ternary operator` you don't. Not unless you hate developers that will work on the code. Note that this can also be you. – VLAZ Jan 15 '19 at 12:49
  • 1
    @JMR I'm sure Bergi knows what if/else condition means. He wants to see it as it'll help him help you by knowing how exactly the ternary needs to look given that your if/else works the way you want it to. – Adrian Jan 15 '19 at 12:51
  • Possible duplicate of [JS Ternary functions with multiple conditions?](https://stackoverflow.com/questions/45022445/js-ternary-functions-with-multiple-conditions) – Abrar Hossain Jan 15 '19 at 13:34

2 Answers2

2
let ele_Partition = records[0].data.meeting ? 
  records[0].data.meeting.partition : records[0].data.meeting.meetingPartition ? 
    records[0].data.meeting.meetingPartition : records[0].data.partition;

If records[0].data.meeting, set the variable to records[0].data.meeting.partition.

Else if records[0].data.meeting.meetingPartition, set the variable to records[0].data.meeting.meetingPartition

Else set the variable to records[0].data.partition.

You should avoid nested ternary operators as they make your code incredibly complicated to read.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Dan
  • 8,041
  • 8
  • 41
  • 72
  • Nested ternaries are only hard to read when not indenting them properly :-) – Bergi Jan 15 '19 at 12:47
  • @Bergi The indentation is mine, I hope you are ok with that – mplungjan Jan 15 '19 at 13:00
  • @mplungjan I saw, it's much better than the original :-) I personally prefer [this kind of indentation](https://stackoverflow.com/a/22998100/1048572) – Bergi Jan 15 '19 at 13:03
1

I would take a single ternary with a default value

let ele_Partition = records[0].data.meeting
        ? records[0].data.meeting.partition
        : records[0].data.meeting.meetingPartition || records[0].data.partition;
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Not super clear if that will shortcut on `records[0].data.meeting.meetingPartition || records[0].data.partition;` – mplungjan Jan 15 '19 at 13:01
  • 1
    @mplungjan techincally, it should. The ternary would be evaluated to one value of `meeting.partition` or `meeting.meetingPartition` and then the result would be ORed with `data.partition`. Assuming the ternary produces a truthy value, it'd just shortcut. However, whether that's the correct logic or not I can't say - OP hasn't clarified. Also, I agree with the more general sentiment that the expression can be confusing to figure out. – VLAZ Jan 15 '19 at 13:04