{"id":60621,"date":"2024-03-27T09:52:23","date_gmt":"2024-03-27T04:22:23","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=60621"},"modified":"2024-03-28T09:55:15","modified_gmt":"2024-03-28T04:25:15","slug":"creating-polyfills-for-map-filter-and-reduce-array-methods","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/creating-polyfills-for-map-filter-and-reduce-array-methods\/","title":{"rendered":"Creating Polyfills for map, filter, and reduce Array Methods"},"content":{"rendered":"<div>\n<p>In this blog post, we embark on a journey to explore the inner workings of these fundamental array methods and learn how to craft polyfills for them. So, let&#8217;s roll up our sleeves and dive into the world of JavaScript polyfills, unlocking the true potential of our favorite programming language.<\/p>\n<\/div>\n<h2 id=\"dcd8\" class=\"ov ow gt be ox oy oz dx pa pb pc dz pd oe pe pf pg oi ph pi pj om pk pl pm pn bj\">Pollyfill for Array.map()<\/h2>\n<div>\n<div>\n<div>\n<div>\n<div><strong>Array.map()<\/strong> syntax:<\/div>\n<pre>array.map((currentValue, index, array) =&gt; { \/\/ function body });<\/pre>\n<\/div>\n<\/div>\n<div>\n<div>\n<p>As we can see, Array.map takes a callback as an argument, and that callback can have 3 arguments &#8211; currentValue, index(optional) and array(optional).<\/p>\n<p>Now Let&#8217; write our polyfill :<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<pre><code>Array.prototype.myMap = function(callbackFun) {\r\n    let newArray = [];\r\n    for (let i = 0; i &lt; this.length; i++) {\r\n        newArray.push(callbackFun(this[i], i, this));\r\n    }\r\n    return newArray;\r\n};<\/code><\/pre>\n<pre><code>let arr = [10, 20, 30];\r\nlet newArr = arr.myMap((x) =&gt; {\r\n    return x * 2;\r\n});\r\nconsole.log(newArr);<\/code><\/pre>\n<div>\n<p>\/\/ output : [ 20, 40, 60 ]<\/p>\n<\/div>\n<h2 id=\"4009\" class=\"ov ow gt be ox oy oz dx pa pb pc dz pd oe pe pf pg oi ph pi pj om pk pl pm pn bj\">Pollyfill for Array.filter()<\/h2>\n<div>\n<div>\n<div>\n<p><strong>Array.filter()<\/strong> syntax:<\/p>\n<pre>array.filter((currentValue, index, array) =&gt; { \/\/ function body });<\/pre>\n<p>&nbsp;<\/p>\n<div>\n<div>\n<p>As we can see, Array.filter takes a callback as an argument, and that callback can have 3 arguments &#8211; currentValue, index(optional) and array(optional)<\/p>\n<p>Now, let&#8217;s write our polyfill:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<pre><code>Array.prototype.myFilter = function(callbackFun) {\r\n    let newArray = [];\r\n    for (let i = 0; i &lt; this.length; i++) {\r\n        if (callbackFun(this[i], i, this)) {\r\n            newArray.push(this[i]);\r\n        }\r\n    }\r\n    return newArray;\r\n};<\/code><\/pre>\n<pre><code>\r\nlet arr = [10, 15, 30];\r\n\r\nlet newArr = arr.myFilter((x) =&gt; {\r\n    return x % 10 == 0;\r\n});\r\nconsole.log(newArr);<\/code><\/pre>\n<p>\/\/Output:\u00a0 [10 , 30]<\/p>\n<h2><span style=\"font-size: 1rem;\">Pollyfill for Array.reduce()<\/span><\/h2>\n<\/div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<p><strong>Array.reduce()<\/strong> syntax:<\/p>\n<pre>array.reduce((accumulator, currentValue, index, array) =&gt; {\r\n\r\n\/\/ function body\r\n\r\n}, initialValue);<\/pre>\n<div>\n<div>\n<p>As we can see, Array.reduce takes 2 arguments, first as a callback, and second an initialValue. Now again that callback can have 4 arguments &#8211; accumulator, currentValue, index(optional) and array(optional).<\/p>\n<p>Now, let&#8217;s write our polyfill:<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<pre><code>Array.prototype.myReduce = function(callbackFun, initialValue) {\r\n    let acc = initialValue;\r\n    for (let i = 0; i &lt; this.length; i++) {\r\n        acc = acc ? callbackFun(acc, this[i], i, this) : this[i];\r\n    }\r\n    return acc;\r\n};<\/code><\/pre>\n<pre><code>\r\nlet arr = [10, 15, 30];\r\nlet newArr = arr.myReduce((accumulator, currentValue) =&gt; {\r\n    return accumulator + currentValue;\r\n}, 0);\r\nconsole.log(newArr);<\/code><\/pre>\n<p>\/\/ output : 55<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Diving into the world of polyfills for essential array methods like <code>map<\/code>, <code>filter<\/code>, and <code>reduce<\/code> provides invaluable insights into JavaScript&#8217;s underlying mechanics and empowers developers to write more robust and cross-compatible code. By understanding how these methods work under the hood and creating polyfills to support older environments, developers can ensure that their applications remain accessible and functional across a wide range of browsers and platforms.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>In this blog post, we embark on a journey to explore the inner workings of these fundamental array methods and learn how to craft polyfills for them. So, let&#8217;s roll up our sleeves and dive into the world of JavaScript polyfills, unlocking the true potential of our favorite programming language. Pollyfill for Array.map() Array.map() syntax: [&hellip;]<\/p>\n","protected":false},"author":1714,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":446},"categories":[3429,4684,1185,3038],"tags":[5701],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60621"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1714"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=60621"}],"version-history":[{"count":11,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60621\/revisions"}],"predecessor-version":[{"id":61018,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60621\/revisions\/61018"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=60621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=60621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=60621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}