Skip to content

Instantly share code, notes, and snippets.

@Flcwl
Last active August 16, 2019 14:45
Show Gist options
  • Save Flcwl/5922e73517030cfd8e0206a189b251fd to your computer and use it in GitHub Desktop.
Save Flcwl/5922e73517030cfd8e0206a189b251fd to your computer and use it in GitHub Desktop.
concatAll, map, filter, forEach
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
}
@Flcwl
Copy link
Author

Flcwl commented Aug 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment