javascript - 如何从 ES6 转换为 ES5(箭头函数)

标签 javascript ecmascript-6 sapui5 ecmascript-5

我正在尝试将 ES6 箭头函数转换为 ES5

我已经尝试更改它但它失去了它的范围,因为我正在使用 this.getView()

this.getModel().read('/CharacteristicSet', {
  filters: this._afilters,
  success: function (oData) {
    oViewModel.setProperty('/charSet', oData.results);

    for (let i = 0; i < oData.results.length; i++) {
      if (oData.results[i].GroupId === sKey) {
        oBinding.filter(this._mFilters[sKey]);
      }
    }

    let aIconTabItems = this.byId('iconTabBar').getItems();
    let aCharacteristics = oData.results;

    for (var j = 0; j < aIconTabItems.length; j++) {
      let count = aCharacteristics.filter(
        obj =>
          obj.GroupId === aIconTabItems[j].getKey() &&
          obj.EquipmentNumber ===
            this.getView()
              .getBindingContext()
              .getProperty('EquipmentNumber'),
      ).length;

      oViewModel.setProperty(`/${aIconTabItems[j].getKey()}`, count);
    }
  }.bind(this),
});

我希望它是 ES5

最佳答案

一个可能的解决方案是使用 Array.filter()thisArg .

thisArg: Optional - Value to use as this when executing callback.

特别是,关于您尝试转换的箭头函数,您可以从 success 回调中传递 this 上下文,以在 filter() 中使用这个:

var count = aCharacteristics.filter(function(obj)
{
    var equipmentNum = this.getView().getBindingContext().getProperty("EquipmentNumber");
    return obj.GroupId === aIconTabItems[j].getKey() && obj.EquipmentNumber === equipmentNum;
}, this /* Here we use the thisArg of filter */).length;

另一种方法是在 loop 之外定义一个变量,就像您对变量 aIconTabItems 所做的那样:

let aIconTabItems = this.byId('iconTabBar').getItems();
let aCharacteristics = oData.results;
let equipmentNum = this.getView().getBindingContext().getProperty('EquipmentNumber');

for (var j = 0; j < aIconTabItems.length; j++)
{
    let count = aCharacteristics.filter(function(obj)
    {
        return obj.GroupId === aIconTabItems[j].getKey() &&
               obj.EquipmentNumber === equipmentNum;
    }).length;

    oViewModel.setProperty(`/${aIconTabItems[j].getKey()}`, count);
}

关于javascript - 如何从 ES6 转换为 ES5(箭头函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56230139/

相关文章:

javascript - 如何用 Jquery 编写这个 if 条件?

javascript - Object.assign 和 ... spread 运算符失败无提示,不添加元素

sapui5 - 如何在 SimpleForm 中隐藏组标题

javascript - 同一列上两个值的自定义排序器

sapui5 - this.getView().byId()、this.byId() 和 sap.ui.getCore().byId() 之间的区别

javascript - 为什么我无法通过 Ajax 获取 WP 自定义字段值或发布 ID?

javascript - 使用 JS/jQuery 从 JsonArray 中提取数据

javascript - 不同顺序的列

javascript - 如何使用扩展语法有效地从关联数组中删除项目

javascript - React.js "this"是保留字