开发者

Prototype startsWith OR

开发者 https://www.devze.com 2022-12-22 16:46 出处:网络
I am tryi开发者_运维问答ng to use Prototype and startsWith but I want to check a number of values and little confused how to do this.

I am tryi开发者_运维问答ng to use Prototype and startsWith but I want to check a number of values and little confused how to do this.

Basically have this code:

if(Category.startsWith("[Test1] " || "Test " || "Test2 ")) { some stuff }

It doesn't appear to be working and just wondering what I am doing wrong ?


You need to do them individually:

if(Category.startsWith("[Test1] ")
   || Category.startsWith("Test ")
   || Category.startsWith("Test2 ")) {
    // some stuff
}

In JavaScript, the expression

"[Test1] " || "Test " || "Test 2 "

...evaluates to "[Test1] ", because || returns the first "truthy" operand.


if(Category.startsWith("[Test1] ")
    ||Category.startsWith("Test ")
    ||Category.startsWith("Test2 ")) 
{ 
    //some stuff
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号