Last active
August 16, 2019 14:45
-
-
Save Flcwl/5922e73517030cfd8e0206a189b251fd to your computer and use it in GitHub Desktop.
concatAll, map, filter, forEach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.forEach = function(cb) { | |
let len = this.length | |
for(let i = 0; i < len; i++) { | |
cb(this[i], i, this) | |
} | |
} | |
Array.prototype.concatAll = function() { | |
let ret = [] | |
// [].push.apply(ret, item); | |
this.forEach(item => ret.push(...item)) | |
return ret | |
} | |
Array.prototype.map = function(cb) { | |
let ret = [] | |
this.forEach((item, index, array) => ret.push(cb(item, index, array))) | |
return ret | |
} | |
Array.prototype.filter = function(cb) { | |
let ret = [] | |
this.forEach((item, index, array) => cb(item, index, array) && ret.push(item)) | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
演示练习: https://codepen.io/flcwl/pen/LYPNayR