-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathobjext.js
53 lines (43 loc) · 1.19 KB
/
objext.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Object.defineProperty(Object.prototype, 'forEach', {
value: function (func) {
Object.keys(this).forEach(key => {
func(this[key], key, this);
});
},
});
Object.defineProperty(Object.prototype, 'reduce', {
value: function (func, value) {
return Object.keys(this).reduce((total, key) => {
return func(total, this[key], key, this);
}, value);
},
});
Object.defineProperty(Object.prototype, 'map', {
value: function (func) {
let ret = {};
Object.keys(this).forEach(key => {
ret[key] = func(this[key], key, this);
});
return ret;
},
});
Object.defineProperty(Object.prototype, 'filter', {
value: function (func = v => Boolean(v)) {
let ret = {};
Object.keys(this).forEach(key => {
if(func.apply && func(this[key], key, this)) {
ret[key] = this[key];
}
});
return ret;
},
});
Object.defineProperty(Object.prototype, 'toArray', {
value: function () {
let ret = [];
for (let key in this) {
ret.push([key, this[key]]);
}
return ret;
},
});