How to nest OR statements in JavaScript? - Stack Overflow

Currently I am creating a filemanager. What I want is to check if the user has selected a video file. T

Currently I am creating a filemanager.

What I want is to check if the user has selected a video file. The file can be mov, f4v, flv, mp4 and swf.

I want to check if my var ext is one of these.

What I have is:

if(ext == ('mov' || 'f4v' || 'flv' || 'mp4' || 'swf'))
{
    //Do something
}

Does anyone know how I can get this working. I don't want to use a switch because I will get a lot of cases.

Currently I am creating a filemanager.

What I want is to check if the user has selected a video file. The file can be mov, f4v, flv, mp4 and swf.

I want to check if my var ext is one of these.

What I have is:

if(ext == ('mov' || 'f4v' || 'flv' || 'mp4' || 'swf'))
{
    //Do something
}

Does anyone know how I can get this working. I don't want to use a switch because I will get a lot of cases.

Share Improve this question asked Oct 5, 2012 at 9:20 Ron van der HeijdenRon van der Heijden 15.1k7 gold badges62 silver badges86 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

You would need to explicitly pare the variable against each of those values.

if( ext === 'mov' || ext === 'f4v' || ... ) { 
}

..but, RegExp to the rescue, we can go like

if( /mov|f4v|flv|mp4|swf/.test( ext ) ) { 
}

you need to split them up, like so:

if(ext === "mov" || ext === "f4v" || ext === "flv" || ext === "mp4" || ext === "swf")
{
    // do stuff
}

you could also consider putting all the different extension in an array and checking whether ext exists in that array

Kinda nice way is this:

var exts = {
     "mov" : null,
     "f4v" : null,
     "flv" : null,
     "mp4" : null,
     "swf" : null,
}

if(ext in exts){
    // world peace
}

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

相关推荐

  • How to nest OR statements in JavaScript? - Stack Overflow

    Currently I am creating a filemanager. What I want is to check if the user has selected a video file. T

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信