如果声明,如何优化3条件

时间:2016-05-25 12:45:20

标签: javascript performance if-statement

您好我必须检查三个条件,是否有任何方法来优化除if else if语句之外的代码。如果我检查所有三种组合,它会更多的代码行,我们有更好的选择来检查所有条件。请任何人帮助我优化代码。

if (req.body.officeId === "null" && req.body.branchId === "null" && req.body.productId === "null") {
  updatedTestData = _.omit(req.body, ['officeId', 'branchId', 'roomId']);
  updatedTestData.officeId = null;
  updatedTestData.office = {};
  updatedTestData.branchId = null;
  updatedTestData.branch = {};
  updatedTestData.productId = null;
  updatedTestData.product = {};

} else if (req.body.officeId === "null" && req.body.branchId === "null" && !req.body.productId === "null") {
  updatedTestData = _.omit(req.body, ['officeId', 'branchId']);
  updatedTestData.officeId = null;
  updatedTestData.office = {};
  updatedTestData.branchId = null;
  updatedTestData.branch = {};

}

6 个答案:

答案 0 :(得分:3)

使用Array.prototype.every()

var condition = [ req.body.officeId, req.body.branchId, req.body.productId]

if (condition.every(item => item === "null") { 

   // do some work

} 

if (condition.every(item => item !== "null") {

  // do other work

}

every会在找到false值时自动退出,因此您只需要经历您的条件,直到失败为止。

答案 1 :(得分:2)

稍微好一点的版本:

if(req.body.officeId === "null" && req.body.branchId === "null"){
    if(req.body.productId === "null"){
        //your first code block
    }else{
        //your second code block
    }
}

答案 2 :(得分:0)

这可能会有所帮助

if (req.body.officeId === "null" && req.body.branchId === "null") {

    if (req.body.productId === "null"){
        updatedTestData = _.omit(req.body, ['officeId', 'branchId', 'roomId']);
        updatedTestData.officeId = null;
        updatedTestData.office = {};
        updatedTestData.branchId = null;
        updatedTestData.branch = {};
        updatedTestData.productId = null;
        updatedTestData.product = {};
        return
    }

    updatedTestData = _.omit(req.body, ['officeId', 'branchId']);
    updatedTestData.officeId = null;
    updatedTestData.office = {};
    updatedTestData.branchId = null;
    updatedTestData.branch = {};


}

答案 3 :(得分:0)

其中2个检查对于if和else都是通用的,所以你可以将它们提取到外部if ...

if (req.body.officeId === "null" && req.body.branchId === "null") {
  if (req.body.productId === "null") {
    updatedTestData = _.omit(req.body, ['officeId', 'branchId', 'roomId']);
    updatedTestData.officeId = null;
    updatedTestData.office = {};
    updatedTestData.branchId = null;
    updatedTestData.branch = {};
    updatedTestData.productId = null;
    updatedTestData.product = {};
  } else {
    updatedTestData = _.omit(req.body, ['officeId', 'branchId']);
    updatedTestData.officeId = null;
    updatedTestData.office = {};
    updatedTestData.branchId = null;
    updatedTestData.branch = {};
  }
}

答案 4 :(得分:0)

var array = ['officeId', 'branchId'];
var body = req.body;
var office = body.officeId;
var branch= body.branchId;
var product= body.productId;
if(office === 'null' && branch === 'null') {
    if(product !== 'null') {
        array.push('roomId');
        updatedTestData = _.omit(body, array);
        testChange(body, 'product', updatedTestData); 
    } else {

    }
}

testChange(req.body, 'office', updatedTestData);
testChange(req.body, 'branch', updatedTestData);

function testChange(object, key, toChange) {
    if(object[key] === 'null') {
        toChange[key + 'Id'] = null;
        toChange[key] = {};
    }
}

答案 5 :(得分:0)

ES 5中的另一种方法:

var isNull = function(items) { 
       if (items === null) return true;
       if (items.length) { 
               return  items.reduce(
                           function(p, c){
                             return ((c===null) && p);
                           }, true); 
       }

       return false;
}

if (isNull([req.body.officeId, 
                 req.body.branchId, 
                 req.body.productId]))  
{
   //
}
else if (isNull([req.body.officeId, 
                 req.body.branchId] && !isNull(req.body.productId))
{
   //
}