I'm using the ternary operator to handle importing data from SQL to Mongo for a variety of fields. For one particular field it's a little trickier than the others, because I want to handle three different conditions:
1
should port to true
, 0
should port to false
, null
should port to null
. This is what I'm trying:
saved: data.saved && data.saved === 1 ? true : data.saved && data.saved === 0 ? false : null
Would this accomplish what I'm needing?
您可以直接检查null
,如果没有将数值转换为boolean。
value === null ? null : Boolean(value)
您可以将值强制为布尔值:
saved: (data === null) ? null : !!data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.