{"id":818,"date":"2012-11-15T15:47:35","date_gmt":"2012-11-15T19:47:35","guid":{"rendered":"http:\/\/benjamin.smedbergs.us\/blog\/?p=818"},"modified":"2012-11-15T15:47:46","modified_gmt":"2012-11-15T19:47:46","slug":"js-array-map-gotcha","status":"publish","type":"post","link":"http:\/\/benjamin.smedbergs.us\/blog\/2012-11-15\/js-array-map-gotcha\/","title":{"rendered":"JS Array.map gotcha"},"content":{"rendered":"<p>I was working on a <a href=\"https:\/\/crash-analysis.mozilla.com\/bsmedberg\/flash-distribution.html\">project<\/a> today and was seeing weird sort behavior and couldn&#8217;t figure out what was going on for a while. I finally discovered that JavaScript <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/JavaScript\/Reference\/Global_Objects\/Array\/map\">Array.map<\/a> wasn&#8217;t returning the values I expected. In particular:<\/p>\n<pre>&gt; \"11.4.402.265\".split('.').map(parseInt)\r\n[11, NaN, NaN, 2]<\/pre>\n<p>I was a little worried that Mozilla was broken, so I asked the JS experts in IRC and got this interesting diagnosis from <a href=\"http:\/\/whereswalden.com\/\">Jeff Walden (Waldo)<\/a>:<\/p>\n<blockquote><p>parseInt takes a second parameter that&#8217;s a radix; map calls its function with (value, index, thisArray)<\/p><\/blockquote>\n<p>This magic behavior of Array.map can be useful if you want to do even-odd behavior or other tasks, but it happened to be my personal footgun today. Be careful! My code now interposes its own function:<\/p>\n<pre>a.split('.').map(function(i) { return parseInt(i); });<\/pre>\n<p>I&#8217;m not sure I really want to know why parseInt(11, 0) returns 11.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a project today and was seeing weird sort behavior and couldn&#8217;t figure out what was going on for a while. I finally discovered that JavaScript Array.map wasn&#8217;t returning the values I expected. In particular: &gt; &#8220;11.4.402.265&#8221;.split(&#8216;.&#8217;).map(parseInt) [11, NaN, NaN, 2] I was a little worried that Mozilla was broken, so I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[266,268,39,265,267],"class_list":["post-818","post","type-post","status-publish","format-standard","hentry","category-mozilla","tag-array","tag-footgun","tag-javascript","tag-js","tag-map"],"_links":{"self":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":3,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"predecessor-version":[{"id":821,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/818\/revisions\/821"}],"wp:attachment":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}