{"id":1181,"date":"2013-02-12T23:20:15","date_gmt":"2013-02-13T05:20:15","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1181"},"modified":"2013-02-15T02:02:35","modified_gmt":"2013-02-15T08:02:35","slug":"code-of-the-day-chop-up-an-array-into-groups-using-underscore-js","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2013\/02\/code-of-the-day-chop-up-an-array-into-groups-using-underscore-js\/","title":{"rendered":"Code of the Day: Chop up an array into groups using Underscore.js"},"content":{"rendered":"<p>Shrink an array into a group of smaller arrays using underscore.js<\/p>\n<p><code><\/p>\n<pre lang='javascript'>\r\n\/\/ Make sure to include underscore.js \r\n_.mixin({\r\n    chunk : function (array, unit) {\r\n        if (!_.isArray(array)) return array;\r\n        unit = Math.abs(unit);\r\n        var results = [],\r\n        length = Math.ceil(array.length \/ unit);\r\n\r\n        for (var i = 0; i < length; i++) {\r\n            results.push(array.slice( i * unit, (i + 1) * unit));\r\n        }\r\n        return results;\r\n    }\r\n});\r\n<\/pre>\n<p><\/code><\/p>\n<p><b>Example:<\/b><br \/>\n<code><\/p>\n<pre lang='javascript'>\r\nJSON.stringify( _.chunk([1,2,3,4,5,6,7,8,9], 2) ); \r\n\/\/ returns \"[[1,2],[3,4],[5,6],[7,8],[9]]\"\r\n<\/pre>\n<p><\/code><\/p>\n<p><iframe style=\"width: 100%; height: 300px\" src=\"http:\/\/jsfiddle.net\/cdYUf\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n<p>Here's the same thing without using Underscore.js<br \/>\n<code><\/p>\n<pre lang='javascript'>\r\nvar chunk = function (array, unit) {\r\n\tif (typeof array !== \"object\")\r\n\t\treturn array;\r\n\tunit = Math.abs(unit);\r\n\tvar results = [],\r\n\tlength = Math.ceil(array.length \/ unit);\r\n\t\r\n\tfor (var i = 0; i < length; i++) {\r\n\t\tresults.push(array.slice(i * unit, (i + 1) * unit));\r\n\t}\r\n\treturn results;\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shrink an array into a group of smaller arrays using underscore.js \/\/ Make sure to include underscore.js _.mixin({ chunk : function (array, unit) { if (!_.isArray(array)) return array; unit = Math.abs(unit); var results = [], length = Math.ceil(array.length \/ unit); for (var i = 0; i < length; i++) { results.push(array.slice( i * unit, (i &hellip; <a href=\"https:\/\/bateru.com\/news\/2013\/02\/code-of-the-day-chop-up-an-array-into-groups-using-underscore-js\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Chop up an array into groups using Underscore.js<\/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":[11],"tags":[30,164,187],"class_list":["post-1181","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-code-of-the-day","tag-javascript","tag-underscore"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1181"}],"version-history":[{"count":5,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1181\/revisions"}],"predecessor-version":[{"id":1187,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1181\/revisions\/1187"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}