-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmy-array.js
36 lines (26 loc) · 814 Bytes
/
my-array.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
28
29
30
31
32
33
34
35
36
// {"category": "Array", "notes": "Implement Array.concatAll and zip"}
/*
Implement Array.concatAll and zip
*/
'use strict';
export class MyArray extends Array {}
Array.prototype.zip = function(combiner, ...arrays) {
let results = [];
let min = arrays.reduce((previous, current) => {
return Math.min(previous.length, current.length);
});
for (let i = 0; i < min; i++) { /* jshint strict: false, -W083 */
let args = arrays.map((array) => { return array[i]; }); /* jshint strict: true, +W083 */
results.push(combiner.apply(this, args));
}
return results;
};
Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
subArray.forEach(function(item) {
results.push(item);
});
});
return results;
};