替代JavaScript中的多个if else语句?

时间:2017-07-07 19:00:28

标签: javascript dictionary if-statement conditional

我有一个输入,其中包含8个项目的下拉列表。根据用户选择的选项,我想将其输入值更改为不同的字符串值。为了做到这一点,我使用了大量的if else语句,这使得它看起来非常笨重,我想尽可能地浓缩这个。我有以下代码:

if (inputFive == "Corporation"){
    inputFive = "534"
} else if (inputFive == "LLC"){
    inputFive = "535"
} else if(inputFive == "LLP"){
    inputFive = "536"
} else if(inputFive == "Partnership"){
    inputFive = "537"
} else if(inputFive == "Sole Proprietorship"){
    inputFive = "538"
} else if(inputFive == "Limited Partnership"){
    inputFive = "539"
} else {
    inputFive = "540"
}

正如你所看到的,这看起来有点老派,我想看看是否有更好/更简单的方法来实现这一点。只是想尽可能地压缩这段代码。我相信他们可能是通过分配键/值对象来创建字典的方法,但我不知道如何正确地执行此操作...所有选项/提示将不胜感激!

5 个答案:

答案 0 :(得分:5)

您可以将对象用作地图:

function getCode(input) {
    var inputMap = {
      "Corporation": "534",
      "LLC": "535",
      "LLP": "536",
      "Partnership": "537",
      "Sole Proprietorship": "538",
      "Limited Partnership": "539"
    };

    var defaultCode = "540";
    
    return inputMap[input] || defaultCode;
}

console.log(getCode("LLP"));
console.log(getCode("Lorem Ipsum"));

答案 1 :(得分:4)

你的直觉是完全正确的。你会这样做:

var mapping = {
    "Corporation": "534",
    "LLC": "535",
    ...
    "default": "540"
}
inputFive = mapping[inputFive] || mapping["default"]

答案 2 :(得分:2)

使用switch语句,这对于您想要针对多个可能值检查单个变量的时间更好:

switch (inputFive) {
  case "Corporation" :
    inputFive = "534";
    break;
  case "LLC":
    inputFive = "535";
    break;
  case "LLP":
    inputFive = "536";
    break;
  case "Partnership":
    inputFive = "537";
    break;
  case "Sole Proprietorship":
    inputFive = "538";
    break;
  case "Limited Partnership":
    inputFive = "539";
    break;
  default:
    inputFive = "540";
    break;
}

答案 3 :(得分:1)

你可能想要某种数组。

businessTypes = [];
businessTypes["Corporation"] = 534;
businessTypes["LLC"] = 535;
businessTypes["LLP"] = 536;
businessTypes["Partnership"] = 537;
businessTypes["Sole Proprietorship"] = 538;
businessTypes["Limited Partnership"] = 539;

然后你可以用以下内容来引用它:

businessId = businessTypes[inputFive] ? businessTypes[inputFive] : 540;
console.log(businessId);

您也可以将其分解为一个函数:

function getBusinessId(type) {
  businessTypes = [];
  businessTypes["Corporation"] = 534;
  businessTypes["LLC"] = 535;
  businessTypes["LLP"] = 536;
  businessTypes["Partnership"] = 537;
  businessTypes["Sole Proprietorship"] = 538;
  businessTypes["Limited Partnership"] = 539;
  return businessTypes[type] ? businessTypes[type] : 540;
}

var businessId = getBusinessId("LLC");
console.log(businessId); // 535

答案 4 :(得分:1)

     selectStringInElement('word','1');


function selectStringInElement(selectionString, elementId)
{

    var $el =  $("#"+elementId);

    $el.contents()
    .filter(function(){ 
            return this.nodeType === 3 // Only text nodes
     })
    .each(function(){
        this.textContent = this.textContent.replace(new RegExp(selectionString,'g'),"<span class='highlight'>"+selectionString+"</span>");
     });

      // Decoding lt/gt
      var html = $("<textarea/>").html($("#1").html()).val() 

      // Replacing original content
      $el.html(html);

     // Attaching onMouseOver events
      console.log($el.find("span.highlight"));
      $el.find("span.highlight").on("mouseover",function(){
            alert($(this).text());
        });
}