【问题标题】:How to reuse this code and make it more dynamic如何重用此代码并使其更具动态性
【发布时间】:2022-09-25 23:55:58
【问题描述】:

我是 Javascript 的新手,虽然我能过得去,但我需要变得更聪明。

我有这段代码重复了大约 7 次,代码中唯一变化的是getMileagePriceMileagegetMileageRate,其他一切都完全相同。

我使用 Google Apps Scripts 中的 \'google.script.run\' 命令调用此函数。 google.script.run.withSuccessHandler(getMileagePrice).getFeesArray();

我怎样才能重用它,所以我使用 6 行代码而不是 42 行代码(7 个函数 x 6 行代码)。

function getMileagePrice(arrayOfValues) {
  var productName = \'Mileage\';
  var filteredArrayOfValues = arrayOfValues.filter(function(r) {
    return r[0] === productName
  });
  getMileageRate = filteredArrayOfValues.reduce((amount, r) => amount + r[1], 0).toFixed(2)
  combinePrices();
}

标签: javascript google-apps-script web-applications


【解决方案1】:

尝试

await getMileagePrice(arrayOfValues,productName)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多