如何在javascript中最小化if else语句与文件类型?

时间:2017-01-22 16:27:23

标签: javascript

我有使用if else语句的代码。 我想尽量减少这种情况。

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";
        }

执行此代码后,输出应为String和Boolean。 我如何使用数组来解决这个问题?

如您所见,在每个if else语句中,它们具有不同数量的表达式。有时它只会检查1种文件类型。有时它会检查多种文件类型。

如何在此问题中使用数组或对象?

4 个答案:

答案 0 :(得分:3)

你应该使用switch

  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 :(得分:1)

也许你可以使用正则表达式:

detailsDf.cache();
System.out.println(detailsDf.count());

答案 2 :(得分:1)

您可以收集数组中的数据,并使用文件类型为hash的对象。然后分配找到的对象所需的值。

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 :(得分: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"} ));