Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果…;为了提高效率,其他人?_Javascript_Function_Refactoring - Fatal编程技术网

Javascript 如果…;为了提高效率,其他人?

Javascript 如果…;为了提高效率,其他人?,javascript,function,refactoring,Javascript,Function,Refactoring,我有一个基于总金额计算费用的应用程序。 它将通过使用[MutationObserver][1]捕获对总量的修改。 当总数更改时,应根据2个数组应用不同的费用:1个用于carttotal步骤和相应的费用 我希望更高效,避免使用所有这些else if语句。在MutationObserver中观察到更改时,我是否应该使用正在调用的函数?我能否有效地利用firstIndex var fees = ["10","20","30"]; var steps = ["99.90","1200.00","3600

我有一个基于总金额计算费用的应用程序。 它将通过使用
[MutationObserver][1]
捕获对总量的修改。 当总数更改时,应根据2个数组应用不同的费用:1个用于carttotal步骤和相应的费用

我希望更高效,避免使用所有这些
else if
语句。在
MutationObserver
中观察到更改时,我是否应该使用正在调用的函数?我能否有效地利用
firstIndex

var fees = ["10","20","30"];
var steps = ["99.90","1200.00","3600.00"];
var observer = new MutationObserver(function (mutations) {
var cartTotal = parseFloat($('.total').html())
//apply different fee based on cart total
if (cartTotal <= 0) {
  shippingFee = 0;
} else if (cartTotal > 0 && cartTotal <= steps[0]) {
  shippingFee = fees[0];
} else if (cartTotal > steps[0] && cartTotal <= steps[1]) {
  shippingFee = fees[1];
} else if (cartTotal > steps[1] && cartTotal <= steps[2]) {
  shippingFee = fees[2];
} else if (cartTotal > steps[2] && cartTotal <= steps[3]) {
  shippingFee = fees[3];
} else if (cartTotal > steps[3] && cartTotal <= steps[4]) {
  shippingFee = fees[4];
} else if (cartTotal > steps[4] && cartTotal <= steps[5]) {
  shippingFee = fees[5];
} else {
  shippingFee = fees[6];
}
shippingFee = parseFloat(shippingFee);

observer.observe(changetotal, {
  childList: true
  });
 });
var费用=[“10”、“20”、“30”];
var步长=[“99.90”、“1200.00”、“3600.00”];
var观察者=新的突变观察者(功能(突变){
var cartotal=parseFloat($('.total').html())
//根据购物车总额申请不同的费用
如果(cartTotal 0&&cartTotal steps[0]&&cartTotal steps[1]&&cartTotal steps[2]&&cartTotal steps[3]&&cartTotal steps[4]&&cartTotal您可以使用和嵌套三元结构

var index = steps.findIndex(v => v <= cartTotal),
    shippingFee = cartTotal <= 0
        ? 0
        : index === -1
            ? fees[6]
            : fees[index];

var index=steps.findIndex(v=>v注意有些条件是多余的——您的if-elseif等价于:

if (cartTotal <= 0) {
  shippingFee = 0;
} else if (cartTotal <= steps[0]) {
  shippingFee = fees[0];
} else if (cartTotal <= steps[1]) {
  shippingFee = fees[1];
} else if (cartTotal <= steps[2]) {
  shippingFee = fees[2];
} else if (cartTotal <= steps[3]) {
  shippingFee = fees[3];
} else if (cartTotal <= steps[4]) {
  shippingFee = fees[4];
} else if (cartTotal <= steps[5]) {
  shippingFee = fees[5];
} else {
  shippingFee = fees[6];
}

if(就代码量或代码速度而言,效率更高?我想说的是代码量。我想避免所有其他的if…我想我可以使用firstIndex();但我不确定如何…等等,是的,对不起-我误读了。它在
步骤之间检查
,但实际上会从
费用
中选择值。您的数组只填充了前三个索引,但您在
if else
块中引用了更多的索引。每个索引的
费用
是否应该增加10?是否应该是
变量dex=steps.findIndex(v=>v>=cartotal)
费用[fees.lenght-1]
费用[6]
更适合我。谢谢!你确实为我指明了正确的方向。
if (cartTotal <= 0) {
  shippingFee = 0;
} else if (cartTotal <= steps[5]) {
  for (var i = 0; i <= 5; i++) {
    if (cartTotal <= steps[i]) {
      shippingFee = fees[i];
      break;
    }
  }
} else {
  shippingFee = fees[6];
}