{"id":42,"date":"2008-07-08T23:20:47","date_gmt":"2008-07-08T13:20:47","guid":{"rendered":"http:\/\/christopherowen.id.au\/blog\/?p=42"},"modified":"2008-07-08T23:20:47","modified_gmt":"2008-07-08T13:20:47","slug":"javascript-reduce","status":"publish","type":"post","link":"https:\/\/christopherowen.au\/blog\/2008\/07\/08\/javascript-reduce\/","title":{"rendered":"JavaScript reduce"},"content":{"rendered":"<p>Tonight <a href=\"http:\/\/www.mattryall.net\">Matt<\/a> posted a <a href=\"http:\/\/mattryall.net\/blog\/2008\/07\/dustins-programming-problem\">JavaScript solution to a problem he saw described<\/a> elsewhere. Here&#8217;s a version that I whipped up to use JavaScript 1.8&#8217;s new <a href=\"http:\/\/developer.mozilla.org\/en\/docs\/Core_JavaScript_1.5_Reference:Objects:Array:reduce\">Array.prototype.reduce function<\/a>.<\/p>\n<pre lang=\"javascript\">\r\nvar arrVal = [\"a\", \"b\", \"c\", \"c\", \"d\", \"e\", \"e\", \"e\", \"e\", \"e\",\r\n  \"f\", \"e\", \"f\", \"e\", \"f\", \"a\", \"a\", \"a\", \"f\", \"f\", \"f\"];\r\n\r\nfunction group(previousValue, currentValue, index, arr)\r\n{\r\n\tif (index > 0  && arr[index - 1] == currentValue)\r\n\t\treturn previousValue.slice(0,-1).concat([previousValue.slice(-1)[0].concat(currentValue)]);\r\n\treturn previousValue.concat([[currentValue]]);\r\n}\r\n\r\nfunction format(previousValue, currentValue, index, arr)\r\n{\r\n\treturn previousValue.concat(\r\n\t\tcurrentValue.length < 3 ? \r\n\t\tcurrentValue.join(\" \") :\r\n\t\tcurrentValue.slice(0,2).join(\" \") + \r\n\t\t\t\" <span>\" + currentValue.slice(2).join(\" \") + \"<span>\");\r\n}\r\n\r\nprint(arrVal.reduce(group, []).reduce(format, []).join(\" \"));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tonight Matt posted a Javascript solution to a problem he saw described elsewhere. Here&#8217;s a version that I whipped up.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"","activitypub_status":"","footnotes":""},"categories":[19],"tags":[57],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-programming","tag-javascript-reduce"],"_links":{"self":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":0,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/christopherowen.au\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}