英文:
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.
public getIcon(document: Document): string {
let mimeType = document.fileType;
let fileExtension = mimeType.split('.')[1];
let iconType = '';
switch (fileExtension) {
case fileExtension === 'jpeg' || 'gif' || 'png' || 'svg' || 'xml' || 'bmp' || 'jpg'|| 'tif' || 'tiff':
iconType = 'picture';
break;
case fileExtension === 'avi' || 'mpg' || 'wmv' || 'mpeg' || 'mp4' || 'mov':
iconType = 'video-folder';
break;
case fileExtension === 'cda' || 'mp3' || 'mpa' || 'wav' || 'wma':
iconType = 'volume';
break;
case fileExtension === 'pdf' || 'ai' || 'pptx':
iconType = 'article';
break;
default:
//includes: .docx , .odt, .rtf, .txt, .xml, .xps
iconType = 'file-text';
break;
}
console.log(iconType);
return iconType;
}
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"):
if (fileExtension === 'jpeg' || 'gif' || 'png' || 'svg' || 'xml' || 'bmp' || 'jpg' || 'tif' || 'tiff') {
iconType = 'picture';
} else if (fileExtension === 'avi' || 'mpg' || 'wmv' || 'mpeg' || 'mp4' || 'mov') {
iconType = 'video-folder';
} else if (fileExtension === 'cda' || 'mp3' || 'mpa' || 'wav' || 'wma') {
iconType = 'volume';
} else if (fileExtension === 'pdf' || 'ai' || 'pptx') {
iconType = 'article';
} else {
iconType = 'file-text';
}
console.log(iconType);
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语句中创建一个单独的情况。
if(fileExtension === "jpg" || fileExtension === "png" ...) {...}
以下还有一些其他选项,以供参考:
将每种图片类型包含在一个数组中,然后检查值是否包含在数组中:
pictureFileExtensions = ["jpg", "gif", ...]
videoFileExtensions = [...]
if (pictureFileExtensions.includes(fileExtension)) {
iconType = "picture"
} else if (videoFileExtensions.includes(fileExtension)) {
iconType = "video"
} else if ...
创建一个以文件扩展名作为键,以图标类型作为值的对象:
const iconTypes = {jpg: "picture", avi: "video", ...}
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.
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:
pictureFileExtensions = [“jpg”, “gif”, …]
videoFileExtensions = […]
If (pictureFileExtension.includes(fileExtension)){
iconType = “picture”
} else if (videoFileExtension.includes(fileExtension)){
iconType = “video”
} else if …
Create an object with the file extension as the key and the icon type as the value:
const iconTypes = {jpg: “picture”, avi: “video”, …}
iconType = iconType[fileExtension]
答案2
得分: 0
你实际上在使用 switch case 时出现了错误。以下是正确的语法:
switch (fileExtension) {
case 'jpeg':
case 'gif':
case 'png':
case 'svg':
case 'xml':
case 'bmp':
case 'jpg':
case 'tif':
case 'tiff':
iconType = 'picture';
break;
case 'avi':
case 'mpg':
case 'wmv':
case 'mpeg':
case 'mp4':
case 'mov':
iconType = 'video-folder';
break;
case 'cda':
case 'mp3':
case 'mpa':
case 'wav':
case 'wma':
iconType = 'volume';
break;
case 'pdf':
case 'ai':
case 'pptx':
iconType = 'article';
break;
default:
// 包括:.docx,.odt,.rtf,.txt,.xml,.xps
iconType = 'file-text';
break;
}
英文:
You're actually using the switch case wrong. Here is the correct syntax:
switch (fileExtension) {
case 'jpeg':
case 'gif':
case 'png':
case 'svg':
case 'xml':
case 'bmp':
case 'jpg':
case 'tif':
case 'tiff':
iconType = 'picture';
break;
case 'avi':
case 'mpg':
case 'wmv':
case 'mpeg':
case 'mp4':
case 'mov':
iconType = 'video-folder';
break;
case 'cda':
case 'mp3':
case 'mpa':
case 'wav':
case 'wma':
iconType = 'volume';
break;
case 'pdf':
case 'ai':
case 'pptx':
iconType = 'article';
break;
default:
//includes: .docx , .odt, .rtf, .txt, .xml, .xps
iconType = 'file-text';
break;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论