有两种方法可以减少常规的javascript数组:
reduce()
(从左到右)reduceRight()
(从右到左)在量角器中,有reduce()
可以从左边的元素到最右边的元素:
对累加器和使用定位器找到的每个元素(从左到右)应用reduce函数。
Protractor中是否有=
方法,如果没有,是否可以基于reduceRight()
实现它?
答案 0 :(得分:3)
Is there reduceRight() method in Protractor?
No, protractor doesn't support this method.
Would it be possible to implement it based on reduce()?
Sure, just replace the arr.reduce
by arr.reduceRight
in the reduce method:
protractor.ElementArrayFinder.prototype.reduceRight = function (reduceFn, initialValue) {
var valuePromise = protractor.promise.fulfilled(initialValue);
return this.asElementFinders_().then(function(arr) {
return arr.reduceRight(function(valuePromise, elementFinder, index) {
return valuePromise.then(function(value) {
return reduceFn(value, elementFinder, index, arr);
});
}, valuePromise);
});
}
答案 1 :(得分:1)
如果您的用例不是太贵,您可以先使用reverse()
,然后再使用常规reduce()
。