Let's Polyfill - map(), filter() and reduce()
The hello world of Polyfills
Map
map is an Array method that takes in a callback and returns an array of items that were returned from the callback
Example:
1const arr = [1, 2, 3, 4]; 2const res = arr.map((el) => el * 2); 3 4console.log(res); // returns [2,4,6,8]
Let’s create our own map
method called myMap
-
myMap()
takes in a parameter which a callback/function. -
It has a results array that gets returned by the
myMap
function. -
The returned values from our
cb
are pushed in theresults
array. -
The
this
here would be the array that we will use thismyMap
function on. -
The traditional
map()
callback can take 3 args. element, index and the source arr. We have done the same.
1function myMap(cb) { 2 // rseults results array that gets returned at the end 3 const results = []; 4 5 for (let i = 0; i < this.length; i++) { 6 // returned values of our cb are pushed in the reults[] 7 // 'this' referes to the passed array 8 results.push(cb(this[i], i, this)); 9 } 10 11 return results; 12} 13 14// Doing this will allow us to use arr.myMap() syntax 15Array.prototype.myMap = myMap; 16 17const arr = [1, 2, 3, 4, 5, 6]; 18const myMapResult = arr.myMap((el, _idx, _arr) => { 19 return el * 2; 20}); 21 22console.log(myMapResult); //[2, 4, 6, 8, 10, 12];
Filter
filter()
is an Array method that takes in a callback and returns an array of items that satisfy the condition provided in our callback
Example:
1const arr = [1, 2, 3, 4]; 2const res = arr.filter((el) => el % 2); // only return even numbers 3 4console.log(res); // [2,4]
Let’s create our own filter
method called myFilter
-
myFilter()
takes in a parameter which a callback/function. -
It has a results array that gets returned at the end.
-
The returned values from our
cb
are pushed in theresults
array. -
The
this
here would be the array that we will use thismyFilter
function on. -
The traditional
filter()
callback can take 3 args. element, index and the source arr. We have done the same.
1function myFilter(cb) { 2 const results = []; 3 4 for (let i = 0; i < this.length; i++) { 5 const cbResult = cb(this[i], i, this); 6 // the returned value of callback is true only then push it to the results 7 if (cbResult) results.push(this[i]); 8 } 9 10 return results; 11} 12 13// Doing this will allow us to use arr.myFilter() syntax 14Array.prototype.myFilter = myFilter; 15 16const arr = [1, 2, 3, 4, 5, 6]; 17 18const foo = [ 19 { name: "S", age: 2 }, 20 { name: "V", age: 3 }, 21]; 22 23const myFilterResult = foo.myFilter((el, _idx, _arr) => { 24 return el.name !== "S"; 25}); 26 27console.log(myFilterResult); // [{ name: "V", age: 3 }]
Reduce
Here the MDN definition of it.
The reduce()
method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
It takes in two important parameters. accumulater
and currentValue
Example:
1const arr = [1, 2, 3, 4]; 2const res = arr.reduce((acc, curr) => { 3 acc += curr; 4 return acc; 5}); // 10 6 7console.log(res); // 10
Lets create our own reduce()
method called myReduce()
-
myReduce()
takes in a parameter which a callback/function. -
It returns a single reduced value.
-
The returned values from our
cb
is assigned to theacc
. -
The
this
here would be the array that we will use thismyReduced
function on. -
The traditional
reduced()
callback can take 4 args. accumulator, currentValue, index and the source arr. We have done the same.
1function myReduce(cb, initialValue) { 2 let acc; 3 let curr; 4 5 if (!this.length && !initialValue) 6 throw new Error("Can't reduce on empty array, provide initial value"); 7 else { 8 // If initialValue is given then acc is that or acc = is the 0th index of this 9 acc = initialValue ? initialValue : this[0]; 10 for (let i = 1; i < this.length; i++) { 11 // current value of the array 12 curr = this[i]; 13 // the retuned cb value is assigned to acc 14 acc = cb(acc, curr, i, this); 15 } 16 } 17 return acc; 18} 19 20// Doing this will allow us to use arr.myReduce() syntax 21Array.prototype.myReduce = myReduce; 22 23const myReduceResult = arr.myReduce((acc, curr, _idx, _arr) => { 24 acc += curr; 25 return acc; 26}); 27 28console.log(myReduceResult); // 21
If you find any errors or edge cases in the above code then please let me know. I am happy to learn about them and add them here.
In the next blog in this series, I’ll try and write our own debounce function from the loadash library
Also, if you guys want to see polyfills of your libs then let me know in the comments.
Hope this blog was helpful to you.