我在数组上进行了es6搜索,由于cordova不允许我使用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;
})];
您只需将lambda替换为一个函数。
编辑:因为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;
};
}
摘自https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex