简体   繁体   English

ES5有效相当于ES6我的lambda搜索吗?

[英]ES5 effectively equivalent to ES6 my lambda search?

我在数组上进行了es6搜索,但由于科尔多瓦不允许我使用lambda,无法弄清楚将其重写为es5 ...

$scope.Contact.title = $scope.doctitles[$scope.doctitles.findIndex(x => x.id == $localStorage.data.contacts[$localStorage.data.itemID].title.id)];
$scope.Contact.title = $scope.doctitles[$scope.doctitles.findIndex(function(x) {
    return x.id == $localStorage.data.contacts[$localStorage.data.itemID].title.id;
})];

You simply replace the lambda with a function. 您只需将lambda替换为函数即可。

EDIT: Since findIndex is also ES6, you can use this polyfill: 编辑:由于findIndex也是ES6,您可以使用此polyfill:

if (!Array.prototype.findIndex) {
  Array.prototype.findIndex = function(predicate) {
    if (this == null) {
      throw new TypeError('Array.prototype.findIndex called on null or undefined');
    }
    if (typeof predicate !== 'function') {
      throw new TypeError('predicate must be a function');
    }
    var list = Object(this);
    var length = list.length >>> 0;
    var thisArg = arguments[1];
    var value;

    for (var i = 0; i < length; i++) {
      value = list[i];
      if (predicate.call(thisArg, value, i, list)) {
        return i;
      }
    }
    return -1;
  };
}

Taken from https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex 取自https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM