How to nest OR statements in JavaScript?

后端 未结 3 589
别那么骄傲
别那么骄傲 2020-12-21 09:12

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<

相关标签:
3条回答
  • 2020-12-21 09:39

    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

    0 讨论(0)
  • 2020-12-21 09:41

    Kinda nice way is this:

    var exts = {
         "mov" : null,
         "f4v" : null,
         "flv" : null,
         "mp4" : null,
         "swf" : null,
    }
    
    if(ext in exts){
        // world peace
    }
    
    0 讨论(0)
  • 2020-12-21 09:53

    You would need to explicitly compare 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 ) ) { 
    }
    
    0 讨论(0)
提交回复
热议问题