How to convert string to boolean in typescript Angular 4

后端 未结 5 812
后悔当初
后悔当初 2021-01-31 13:33

I know am not the first to ask this and as I mentioned in my title ,I am trying to convert string value boolean .

I have previously put some values into local storage,No

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 13:57

    I have been trying different values with JSON.parse(value) and it seems to do the work:

    // true
    Boolean(JSON.parse("true"));
    Boolean(JSON.parse("1"));
    Boolean(JSON.parse(1));
    Boolean(JSON.parse(true));
    
    // false
    Boolean(JSON.parse("0")); 
    Boolean(JSON.parse(0));
    Boolean(JSON.parse("false"));
    Boolean(JSON.parse(false));
    

提交回复
热议问题