我应该如何以更好的方式编写以下javascript函数?

  function showIssueCount(issueSummary){
    if(issueSummary["CRITICAL"] >= 1){
      return {value:issueSummary["CRITICAL"], class:"CRITICAL" };
    }else if(issueSummary["MAJOR"] >= 1 ){
      return {value:issueSummary["MAJOR"], class:"MAJOR" };
    }else if (issueSummary["MINOR"] >= 1){
      return {value:issueSummary["MINOR"], class:"MINOR" };
    }else{
      return {vaue:0, class:""}
    }
  }

最佳答案

像这样的东西

function showIssueCount( obj )
{
    var priority = [ 'CRITICAL', 'MAJOR', 'MINOR' ];
    for( var counter = 0; counter < priority.length; counter++ )
    {
        if ( obj[ priority[ counter ] ] >= 1 )
        {
            return { value : obj[ priority[ counter ] ], class: priority[ counter ] };
        }
    }
    return {value:0, class:""}
}

关于javascript - javascript如果对数组长度标志进行语句优化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34197167/

10-13 09:09