Created
July 24, 2020 14:34
-
-
Save Dan-Nolan/6fc417ad24b2cdbf0165513e496c70c7 to your computer and use it in GitHub Desktop.
Array Method Synonyms
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
// For learning purposes only! | |
// Don't pollute the Array prototype in an application | |
// map synonym | |
Array.prototype.transform = function(callback) { | |
const newArr = []; | |
for(let i = 0; i < this.length; i++) { | |
newArr.push(callback(this[i])); | |
} | |
return newArr; | |
} | |
// filter synonym | |
Array.prototype.takeOnly = function(callback) { | |
const newArr = []; | |
for(let i = 0; i < this.length; i++) { | |
if(callback(this[i])) { | |
newArr.push(this[i]); | |
} | |
} | |
return newArr; | |
} | |
// reduce synonym | |
Array.prototype.aggregate = function(callback, initalValue) { | |
let accumulator = initalValue; | |
for(let i = 0; i < this.length; i++) { | |
const el = this[i]; | |
accumulator = callback(accumulator, el); | |
} | |
return accumulator; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment