{"id":1727,"date":"2018-03-14T22:35:32","date_gmt":"2018-03-15T03:35:32","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1727"},"modified":"2018-03-14T22:35:32","modified_gmt":"2018-03-15T03:35:32","slug":"javascript-use-array-map-method","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/03\/14\/javascript-use-array-map-method\/","title":{"rendered":"JavaScript: How to use the array.map( ) method"},"content":{"rendered":"<p>The JavaScript <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">map()<\/code>method creates a <strong>new array<\/strong> with the results of calling a provided function on <strong>every element<\/strong> in this array. For example, lets say you have a small array of numbers called &#8220;numbers&#8221;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var numbers = [1, 4, 9];<\/pre>\n<p>We can use <strong>.map <\/strong>on this array to find the square root of each of the elements in numbers AND assign those results to a new array called &#8216;roots&#8217;:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var roots = numbers.map(Math.sqrt);<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">\/\/ roots now contains [1, 2, 3], numbers still contains [1, 4, 9]<\/pre>\n<p>Here is another example of using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">map()<\/code>from the mozilla JavaScript reference site. This example shows how to use map on a String to get an array of bytes in the ASCII encoding representing the character values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var map = Array.prototype.map;\r\nvar a = map.call('Hello World', function(x) { \r\n  return x.charCodeAt(0); \r\n});\r\nconsole.log(a);<\/pre>\n<p>which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]<\/pre>\n<p>These values can be independently verified with my JavaScript Ascii Engine!<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"hLtnkdqcbO\"><p><a href=\"http:\/\/bluegalaxy.info\/codewalk\/2017\/11\/16\/javascript-web-app-javascript-ascii-engine\/\">JavaScript Web App: JavaScript ASCII Engine<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/bluegalaxy.info\/codewalk\/2017\/11\/16\/javascript-web-app-javascript-ascii-engine\/embed\/#?secret=hLtnkdqcbO\" data-secret=\"hLtnkdqcbO\" width=\"600\" height=\"338\" title=\"&#8220;JavaScript Web App: JavaScript ASCII Engine&#8221; &#8212; Chris Nielsen Code Walk\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>For more information about the .map() function see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/jsref\/jsref_map.asp\">https:\/\/www.w3schools.com\/jsref\/jsref_map.asp<\/a><br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The JavaScript map()method creates a new array with the results of calling a provided function on every element in this array. For example, lets say you have a small array of numbers called &#8220;numbers&#8221;: var numbers = [1, 4, 9]; We can use .map on this array to find the square root of each of &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/03\/14\/javascript-use-array-map-method\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to use the array.map( ) method<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[45],"class_list":["post-1727","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1727"}],"version-history":[{"count":4,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1727\/revisions"}],"predecessor-version":[{"id":1732,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1727\/revisions\/1732"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}