-1

i am getting error if i use while creating an object.

"ok_count": s.status !== (-3 && -2 && -1 && 0 && 1) ? "1" : "0"

Do anyone have any idea how can i use conditional operator with Logical &

  • There is no syntax error in the code you posted, unless `s` is `undefined`. The `&&` expression does not make any sense, but it is syntactically correct. – Pointy Jul 23 '21 at 12:50
  • `(-3 && -2 && -1 && 0 && 1)` will always be `0`. The conditional operator is irrelevant here. – Quentin Jul 23 '21 at 12:50
  • @Quentin but i am creating an an object here and then pushing it into an array, can you please suggest how can i check for conditions here if conditional operator doesnt work here. – Bhuta Gorilla Jul 23 '21 at 12:53
  • @BhutaGorilla — The conditional operator is fine. The problem is the condition. The duplicate question covers that. – Quentin Jul 23 '21 at 12:54

1 Answers1

2

You have to individually compare each value

"ok_count": (s.status !== -3 && s.status !== -2 && s.status !== -1 && s.status !==0 && s.status !==1) ? "1" : "0"

or you can use a shortcut with an array includes method like

"ok_count": ![-3, -2, -1, 0, 1].includes(s.status) ? "1" : "0"

or if your range is continues you can use >= and <= operators

"ok_count": (s.status >= -3 && s.status <= 1)? "1" : "0"
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400