最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript parsing boolean - Stack Overflow

programmeradmin1浏览0评论

in the response from my server I get a JSON object. It has a boolean flag.

if(file.showInTable == 'true') {

} 

But, even if showInTable is set to false, I get inside that code block. How to cope with that ?

I tried:

if(file.showInTable == 'true')
if(file.showInTable)
if(Boolean(file.showInTable))

Edit

as Ghommey has mentioned, I've used the 2nd option to check that value. Even if the parions statement returns false, it also gets inside the code. See the pic below

in the response from my server I get a JSON object. It has a boolean flag.

if(file.showInTable == 'true') {

} 

But, even if showInTable is set to false, I get inside that code block. How to cope with that ?

I tried:

if(file.showInTable == 'true')
if(file.showInTable)
if(Boolean(file.showInTable))

Edit

as Ghommey has mentioned, I've used the 2nd option to check that value. Even if the parions statement returns false, it also gets inside the code. See the pic below

Share Improve this question edited Aug 17, 2012 at 10:30 Tony asked Aug 17, 2012 at 8:51 TonyTony 12.7k38 gold badges129 silver badges232 bronze badges 7
  • 2 Please post the JSON response you get. – Bojangles Commented Aug 17, 2012 at 8:53
  • What is typeof file.showInTable ? – xdazz Commented Aug 17, 2012 at 8:53
  • Is it set to the boolean value false or the string 'false'? – Anthony Grist Commented Aug 17, 2012 at 8:54
  • 1 Duplicate of stackoverflow./questions/263965/… – Sergii Stotskyi Commented Aug 17, 2012 at 8:54
  • Anthony Grist: it is set to false or true (as bool) – Tony Commented Aug 17, 2012 at 8:58
 |  Show 2 more ments

2 Answers 2

Reset to default 2

it is set to false or true (as bool) - Tony

Why do you pare a boolean as a string?

Just pare it as a boolean:

if(file.showInTable === true) {

} 

or

if(file.showInTable !== false) {

} 

This is ugly, but why not?

if (file.showInTable === "false") file.showInTable = false;
发布评论

评论列表(0)

  1. 暂无评论