2

Using If statement to filter data with multiple string values

i want to sort the file extension in one line because if i dont do that i have to write multiple lines of code which i think is a waste of time. can i get some help here

if (fileExtension === "jpg"||"Jpeg"||"png"||"svg"){
//do something 
}
else{
//do something else
}

if have so file extension jpg then its directly switching to else statement and if i give just jpg i.e if (fileExtension === "jpg") then the code runs fine i want to use multiple.

  • 4
    Possible duplicate of [Javascript: The prettiest way to compare one value against multiple values](https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – Ivar Apr 15 '19 at 11:49
  • 2
    `if (["jpg","jpeg","png","svg"].includes(fileExtension.toLower​Case()))` Did you try searching before asking? there are tons of questions like this, having tons of **great** answers to check. – briosheje Apr 15 '19 at 11:49
  • You can use `.indexOf()` like `if(["jpg","jpeg","png","svg"].indexOf(fileExtension) > -1)` – Mamdouh Saeed Apr 15 '19 at 11:53

6 Answers6

5

"jpg"||"Jpeg"||"png"||"svg" is an expression which will evaluate to first truthy value. Here "jpg" is first truthy value so while whole expression becomes "jpg". See Short-circuit evaluation .

You can store all the values in array and then use some() on array and compare it with fileExtension

"Jpeg" may cause some problem. capital J. I would suggest you to change all the values to lower case and then compare inside some()

let fileExtension = "svg"

if (["jpg","jpeg","png","svg"].some(x => fileExtension.toLowerCase() === x)){
  console.log("found")
}
else{
  cosnole.log("not found")
}

Note: You can also use includes() in this case. But I would prefer some() because it allows user to compare with multiple conditions

Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • 3
    Well done for explaining why the code didn't work. As a side note, I would check for lowercase / uppercase values as an extra feature, since the issue likely would happen with "Jpeg". – briosheje Apr 15 '19 at 11:52
3

Everyone else is using an array, so here's an example with a regex [1] [2] instead:

function testExtension(fileExtension) {
  if (/jpg|jpeg|png|svg/.test(fileExtension.toLowerCase())) {
    console.log(`${fileExtension} found`);
  } else {
    console.log(`${fileExtension} not found`); 
  }
}

testExtension('mpg');
testExtension('jpg');
testExtension('svg');
Andy
  • 61,948
  • 13
  • 68
  • 95
0
if (fileExtension === "jpg"|| fileExtension ==="Jpeg" || fileExtension ==="png" || fileExtension ==="svg"){
//do something 
}
else{
//do something else
}

you should check each string with the fileExtension.

0

Includes can solve your problem.

if (["jpg", "Jpeg", "png", "svg"].includes(fileExtension)) {
   //do something 
} else{
   //do something else
}
Remi Sture
  • 12,000
  • 5
  • 22
  • 35
0

You could use includes prototype of Array:

if (['jpg', 'Jpeg', 'png', 'svg'].includes(fileExtension)) {
    ...
}

You can also add toLowerCase(), to be a bit safer:

if (['jpg', 'jpeg', 'png', 'svg'].includes(fileExtension.toLower​Case())) {
    ...
}

As an alternative (but much less readable) series of OR conditions:

if (fileExtension === 'jpg' || fileExtension === 'Jpeg' 
    || fileExtension === 'png' || fileExtension ==='svg'){

as you can see it will be less readable, with more arguments.

Beri
  • 11,470
  • 4
  • 35
  • 57
0

Using .indexOf() which returns the item position, if the item doesn't exist it returns -1

if(["jpg","jpeg","png","svg"].indexOf(fileExtension) > -1){
//do something...
}

Another way Using .filter()

if(["jpg","jpeg","png","Svg"].filter(function(i){return i.toLowerCase() === fileExtension.toLowerCase()}).length == 1){
    //do something...
    }
Mamdouh Saeed
  • 2,302
  • 1
  • 9
  • 11