{"id":55,"date":"2008-07-02T18:26:04","date_gmt":"2008-07-02T23:26:04","guid":{"rendered":"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/02\/learn-to-map\/"},"modified":"2008-07-02T18:26:04","modified_gmt":"2008-07-02T23:26:04","slug":"learn-to-map","status":"publish","type":"post","link":"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/02\/learn-to-map\/","title":{"rendered":"Learn to Map"},"content":{"rendered":"<p>As a follow-up to <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/02\/comma-separated-values\/\">my previous post about how to create comma separated strings from arrays<\/a>, here&#8217;s another array tip.<\/p>\n<p>Let&#8217;s take the previous example of the Tags array. Except this time, Tags contains the IDs, which correspond to the textual string.<\/p>\n<pre>Tags[0] = 23\r\nTags[1] = 42\r\nTags[2] = 314<\/pre>\n<p>Rather than write a for-loop to look up each value, let&#8217;s pretend we&#8217;ve already got a function getTagTextByID(), which accepts the tag ID, and returns the tag text.<\/p>\n<pre>Tags = Tags.map(getTagTextByID)<\/pre>\n<p>Now tags has the text for the tags.<\/p>\n<p>Map is a fairly common function for operating on arrays. As with the previous tip, Map is available in many languages. <a href=\"http:\/\/us3.php.net\/array_map\" title=\"PHP documentation for array_map\">PHP offers array_map()<\/a>, and <a href=\"http:\/\/developer.mozilla.org\/en\/docs\/Core_JavaScript_1.5_Reference:Objects:Array:map\" title=\"documentation for javascript's array map function\">JavaScript offers Array.map()<\/a>. Check your favorite language for this function, it makes code cleaner.<\/p>\n<p>As an added bonus, <a href=\"http:\/\/labs.google.com\/papers\/mapreduce.html\">Map has a starring role in MapReduce<\/a>, Google&#8217;s favorite technique. Shouldn&#8217;t you get to know Map too?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a follow-up to my previous post about how to create comma separated strings from arrays, here&#8217;s another array tip. Let&#8217;s take the previous example of the Tags array. Except this time, Tags contains the IDs, which correspond to the textual string. Tags[0] = 23 Tags[1] = 42 Tags[2] = 314 Rather than write a &hellip; <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/02\/learn-to-map\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn to Map&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[150,152,151],"_links":{"self":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/55"}],"collection":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}