JavaScript parsing boolean - Stack Overflow

in the response from my server I get a JSON object. It has a boolean flag.if(file.showInTable == '

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745219531a4617179.html

相关推荐

  • JavaScript parsing boolean - Stack Overflow

    in the response from my server I get a JSON object. It has a boolean flag.if(file.showInTable == '

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信