Skip to content

Instantly share code, notes, and snippets.

@Dan-Nolan
Created July 24, 2020 14:34
Show Gist options
  • Save Dan-Nolan/6fc417ad24b2cdbf0165513e496c70c7 to your computer and use it in GitHub Desktop.
Save Dan-Nolan/6fc417ad24b2cdbf0165513e496c70c7 to your computer and use it in GitHub Desktop.
Array Method Synonyms
// 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