2017-01-22 31 views
1

我有這個代碼,使用if語句。 我想盡量減少這個。如何最小化if語句與JavaScript中的文件類型?

var str = ""; 
     var isPreview=true; 
     var dl = Files.findOne({_id:item._id}).link(); 
     var filename = item.name.split('.').pop().toLowerCase(); 
     if(filename == "wav" || filename == "mp3" || filename == "ogg") { 
      str = "fa fa-file-audio-o"; 
      isPreview=false; 
     } 
     else 
     if(filename == "jpg" || filename == "jpeg" || filename == "gif" || filename == "bmp" || filename == "png") 
      str = "fa fa-file-image-o"; 
     else 
     if(filename == "flv" || filename == "wmv" || filename == "mp4" || filename == "3gp" || filename == "webm") { 
      str = "fa fa-file-movie-o"; 
      isPreview=false; 
     } 
     else 
     if(filename == "pdf") 
      str = " fa fa-file-pdf-o"; 
     else 
     if(filename == "txt" || filename == "rtf") 
      str = " fa fa-file-text-o"; 
     else 
     if(filename == "xls" || filename == "xlsx") { 
      str = " fa fa-file-excel-o"; 
      isPreview=false; 
     } 
     else 
     if(filename == "doc" || filename == "docx") { 
      str = " fa fa-file-word-o"; 
      isPreview = false; 
     } 
     else 
     if(filename == "ppt" || filename == "pptx") { 
      str = " fa fa-file-powerpoint-o"; 
      isPreview = false; 
     } 
     else 
     if(filename == "zip" || filename == "rar") { 
      str = " fa fa-file-zip-o"; 
      isPreview = false; 
     } 
     else{ 
      isPreview=false; 
      str = " fa fa-file"; 
     } 
     if(Files.findOne({_id:item._id}).infected){ 
      isPreview=false; 
      str = " fa fa-ban"; 
     } 

執行此代碼後,輸出應該是一個字符串和一個布爾值。 如何使用數組來解決這個問題?

正如您所看到的,在每個if else語句中,它們具有不同數量的表達式。有時它只會檢查一種文件類型。有時它會檢查多種文件類型。

如何在這個問題中使用數組或對象?

+0

這不是最小化,但可以將其更改爲switch/case語句。看起來更乾淨(特別是因爲你只有文件名上的if/else分支) –

+0

是的,這個評論也幫助我。但我更喜歡妮娜的答案,以便更多地減少答案。 – JMA

+0

我還向帶有switch語句的答案添加了+1。 – JMA

回答

1

您可以收集數組中的數據並使用文件類型爲哈希的對象。然後分配找到的對象的所需值。

var fileTypes = [ 
     { type: ["wav", "mp3", "ogg"], preview: false, value: "fa fa-file-audio-o" }, 
     { type: ["jpg", "jpeg", "gif", "bmp", "png"], preview: true, value: "fa fa-file-image-o" }, 
     { type: ["flv", "wmv", "mp4", "3gp", "webm"], preview: false, value: "fa fa-file-movie-o" }, 
     { type: ["pdf"], preview: false, value: " fa fa-file-pdf-o" }, 
     { type: ["txt", "rtf"], preview: false, value: " fa fa-file-text-o" }, 
     { type: ["doc", "docx"], preview: false, value: " fa fa-file-excel-o" }, 
     { type: ["xls", "xlsx"], preview: false, value: " fa fa-file-word-o" }, 
     { type: ["ppt", "pptx"], preview: false, value: " fa fa-file-powerpoint-o" }, 
     { type: ["zip", "rar"], preview: false, value: " fa fa-file-zip-o" }, 
     { type: ["default"], preview: false, value: " fa fa-file" }, 
    ], 
    hash = {}; 

fileTypes.forEach(function (a) { 
    a.type.forEach(function (b) { 
     hash[b] = a; 
    }); 
}); 

var fileType = hash[filename] || hash.default, 
    str = fileType.value, 
    isPreview = fileType.preview; 
3

你應該使用開關爲此

var filename = item.name.split('.').pop().toLowerCase(); 
    switch(filename){ 
    case 'wav': 
    case 'mp3': 
    case 'ogg': 
     str = "fa fa-file-audio-o"; 
     isPreview=false; 
    break; 

    case 'jpg': 
    case 'jpeg':  
    case 'gif': 
    case 'bmp': 
    case 'png':  
     str = "fa fa-file-image-o"; 
     break; 

    case 'flv': 
    case 'wmv': 
    case 'mp4':   
    case '3gp': 
    case 'webm': 
     str = "fa fa-file-movie-o"; 
     isPreview=false; 
     break; 


    ....... 


    default: 
     // your default value 
     break; 
    }  
1

也許你可以使用正則表達式:

switch(true) { 
    case /^(jpg|jpeg|gif|bmp|png)$/.test(filename): 
     // image 
     break; 
    case /^(flv|wmv|mp4|3gp|webm)$/.test(filename): 
     // movie 
     break; 
    // ... 
}  
0

一個數據驅動的解決方案:

const CLASS_AND_PREVIEW_LIST = [ 
 
    [["wav", "mp3", "ogg"], [" fa fa-file-audio-o", false]], 
 
    [["jpg", "jpeg", "gif", "bmp", "png"], [" fa fa-file-image-o", true]], 
 
    [["flv", "wmv", "mp4", "3gp", "webm"], [" fa fa-file-movie-o", false]], 
 
]; 
 
const CLASS_AND_PREVIEW_INFECTED = { 
 
    cssClass: " fa fa-ban", 
 
    isPreview: false 
 
}; 
 
const CLASS_AND_PREVIEW = {}; 
 
    
 
CLASS_AND_PREVIEW_LIST.forEach(([exts, value]) => { 
 
    const fileTypeObj = { 
 
    cssClass: value[0], 
 
    isPreview: value[1] 
 
    }; 
 
    exts.forEach(ext => { 
 
    CLASS_AND_PREVIEW[ext] = fileTypeObj; 
 
    }); 
 
}); 
 

 
function getClassAndPreviewForExt(ext) { 
 
    if (CLASS_AND_PREVIEW.hasOwnProperty(ext)) { 
 
    return CLASS_AND_PREVIEW[ext]; 
 
    } 
 
    return null; 
 
} 
 

 
function getClassAndPreview(fileObj) { 
 
    if (fileObj.infected) { 
 
    return CLASS_AND_PREVIEW_INFECTED; 
 
    } 
 
    return getClassAndPreviewForExt(fileObj.ext); 
 
} 
 

 
console.log(getClassAndPreview({ infected: false, ext: "avi"})); 
 
console.log(getClassAndPreview({ infected: false, ext: "bmp"})); 
 
console.log(getClassAndPreview({ infected: true, ext: "any"}));