-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreduce.js
27 lines (26 loc) · 894 Bytes
/
reduce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/** Polyfill for Array.prototype.reduce()
* arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])
* */
if(!Array.prototype.reduce1) {
Array.prototype.reduce1 = function(callback, initialValue) {
if(this === null) {
throw new TypeError('this is null or not defined');
}
if(typeof callback !== 'function') {
throw new TypeError('callback is not a function');
}
if(this.length === 0) {
throw new TypeError('Reduce of empty array with no initial value');
}
var result = this[0];
var start = 1;
if(arguments.length >= 2) {
result = arguments[1]; //initialValue
start = 0;
}
for(var i = start; i < this.length; i++) {
result = callback(result, this[i], i, this);
}
return result;
}
}