Switch语句和等效的if-else循环始终返回错误的值。

huangapple go评论105阅读模式
英文:

Switch statement and equivalent if else loop always return wrong value

问题

我目前正在尝试使用Angular框架在文档旁边显示不同文件类型的图标。然而,无论我将文件类型设置为txt或jpg,它总是返回file-text作为图标类型的默认值。

如果我尝试进行调试,我可以看到尽管fileExtension的值是"jpg",它却返回默认的图标类型"file-text"。

我还尝试了使用简单的if-else循环等效方法,但当我这样做时,它似乎总是对所有情况返回第一个if条件的值(例如:即使fileExtension等于"txt",也返回"picture")。

我真的很困惑为什么它不进入正确的case。任何帮助将不胜感激!

英文:

I am currently trying to display a different icon next to a document for different file types using Angular framework. However, no matter what file type I set as the fileExtension, be it txt or jpg as examples, it always goes to the default value of returning file-text as the iconType.

  1. public getIcon(document: Document): string {
  2. let mimeType = document.fileType;
  3. let fileExtension = mimeType.split('.')[1];
  4. let iconType = '';
  5. switch (fileExtension) {
  6. case fileExtension === 'jpeg' || 'gif' || 'png' || 'svg' || 'xml' || 'bmp' || 'jpg'|| 'tif' || 'tiff':
  7. iconType = 'picture';
  8. break;
  9. case fileExtension === 'avi' || 'mpg' || 'wmv' || 'mpeg' || 'mp4' || 'mov':
  10. iconType = 'video-folder';
  11. break;
  12. case fileExtension === 'cda' || 'mp3' || 'mpa' || 'wav' || 'wma':
  13. iconType = 'volume';
  14. break;
  15. case fileExtension === 'pdf' || 'ai' || 'pptx':
  16. iconType = 'article';
  17. break;
  18. default:
  19. //includes: .docx , .odt, .rtf, .txt, .xml, .xps
  20. iconType = 'file-text';
  21. break;
  22. }
  23. console.log(iconType);
  24. return iconType;
  25. }

If I try debugging, I can see that despite the value of fileExtension being "jpg", it returns the default icon-type of "file-text". Image of debugging with breakpoints

I also tried using a simple if else loop equivalent but when I do that it seems to always return the first if case value for all (e.g: picture even when fileExtension = "txt"):

  1. if (fileExtension === 'jpeg' || 'gif' || 'png' || 'svg' || 'xml' || 'bmp' || 'jpg' || 'tif' || 'tiff') {
  2. iconType = 'picture';
  3. } else if (fileExtension === 'avi' || 'mpg' || 'wmv' || 'mpeg' || 'mp4' || 'mov') {
  4. iconType = 'video-folder';
  5. } else if (fileExtension === 'cda' || 'mp3' || 'mpa' || 'wav' || 'wma') {
  6. iconType = 'volume';
  7. } else if (fileExtension === 'pdf' || 'ai' || 'pptx') {
  8. iconType = 'article';
  9. } else {
  10. iconType = 'file-text';
  11. }
  12. console.log(iconType);
  13. return iconType;

I am just really confused as to why it doesn't go to the proper case. Any help would be greatly appreciated!

答案1

得分: 1

你需要在每次条件检查的if语句中重新表述左侧,或者为每个单独的文件扩展类型在switch语句中创建一个单独的情况。

  1. if(fileExtension === "jpg" || fileExtension === "png" ...) {...}

以下还有一些其他选项,以供参考:

将每种图片类型包含在一个数组中,然后检查值是否包含在数组中:

  1. pictureFileExtensions = ["jpg", "gif", ...]
  2. videoFileExtensions = [...]
  3. if (pictureFileExtensions.includes(fileExtension)) {
  4. iconType = "picture"
  5. } else if (videoFileExtensions.includes(fileExtension)) {
  6. iconType = "video"
  7. } else if ...

创建一个以文件扩展名作为键,以图标类型作为值的对象:

  1. const iconTypes = {jpg: "picture", avi: "video", ...}
  2. iconType = iconTypes[fileExtension]
英文:

You need to restate the left side of your conditional check in your if statement each time; or have a separate case for each individual file extension type in your switch.

  1. if(fileExtension === jpg || fileExtension === png …) {…}

Here are a few other options as well in case you’re interested:

Include each picture type in an array, and check to see if the value is included in the array:

  1. pictureFileExtensions = [“jpg”, gif”, …]
  2. videoFileExtensions = […]
  3. If (pictureFileExtension.includes(fileExtension)){
  4. iconType = picture
  5. } else if (videoFileExtension.includes(fileExtension)){
  6. iconType = video
  7. } else if

Create an object with the file extension as the key and the icon type as the value:

  1. const iconTypes = {jpg: picture”, avi: video”, …}
  2. iconType = iconType[fileExtension]

答案2

得分: 0

你实际上在使用 switch case 时出现了错误。以下是正确的语法:

  1. switch (fileExtension) {
  2. case 'jpeg':
  3. case 'gif':
  4. case 'png':
  5. case 'svg':
  6. case 'xml':
  7. case 'bmp':
  8. case 'jpg':
  9. case 'tif':
  10. case 'tiff':
  11. iconType = 'picture';
  12. break;
  13. case 'avi':
  14. case 'mpg':
  15. case 'wmv':
  16. case 'mpeg':
  17. case 'mp4':
  18. case 'mov':
  19. iconType = 'video-folder';
  20. break;
  21. case 'cda':
  22. case 'mp3':
  23. case 'mpa':
  24. case 'wav':
  25. case 'wma':
  26. iconType = 'volume';
  27. break;
  28. case 'pdf':
  29. case 'ai':
  30. case 'pptx':
  31. iconType = 'article';
  32. break;
  33. default:
  34. // 包括:.docx,.odt,.rtf,.txt,.xml,.xps
  35. iconType = 'file-text';
  36. break;
  37. }
英文:

You're actually using the switch case wrong. Here is the correct syntax:

  1. switch (fileExtension) {
  2. case 'jpeg':
  3. case 'gif':
  4. case 'png':
  5. case 'svg':
  6. case 'xml':
  7. case 'bmp':
  8. case 'jpg':
  9. case 'tif':
  10. case 'tiff':
  11. iconType = 'picture';
  12. break;
  13. case 'avi':
  14. case 'mpg':
  15. case 'wmv':
  16. case 'mpeg':
  17. case 'mp4':
  18. case 'mov':
  19. iconType = 'video-folder';
  20. break;
  21. case 'cda':
  22. case 'mp3':
  23. case 'mpa':
  24. case 'wav':
  25. case 'wma':
  26. iconType = 'volume';
  27. break;
  28. case 'pdf':
  29. case 'ai':
  30. case 'pptx':
  31. iconType = 'article';
  32. break;
  33. default:
  34. //includes: .docx , .odt, .rtf, .txt, .xml, .xps
  35. iconType = 'file-text';
  36. break;
  37. }

huangapple
  • 本文由 发表于 2023年6月19日 12:30:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定