简体   繁体   English

什么是将python布尔值转换为javascript布尔文字的更简洁方法?

[英]What is a more succinct way of converting python boolean to javascript boolean literals?

I want to convert a python boolean into JS's boolean literal. 我想将python布尔值转换为JS的布尔文字。 This is what I am working with: 这就是我正在使用的:

store = dict(vat=True)

if store['vat']:
    store.update({'vat': 'true'})
else:
    store.update({'vat': 'false'})

Is there a more less verbose way to replace this code snippet ? 是否有更简洁的方法来替换此代码段?

>>> store['vat'] = json.dumps(store['vat'])
>>> store
{'vat': 'true'}

In JS a positive integer value is effectively true, and 0 (zero) is false. 在JS中,正整数值实际上为真,0(零)为假。

You may try passing 0 as a JS false, and 1 as a JS true (do not use negative values) 您可以尝试将0作为JS false传递,将1作为JS true传递(不要使用负值)

>1 == true
true
>0 == true
false
>0 == false
true
>1 == false
false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM