{"id":334,"date":"2016-07-04T22:59:52","date_gmt":"2016-07-04T21:59:52","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=334"},"modified":"2022-07-25T08:35:45","modified_gmt":"2022-07-25T07:35:45","slug":"optimized-rows-in-javascript","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/optimized-rows-in-javascript\/","title":{"rendered":"Optimized rows in javascript"},"content":{"rendered":"<pre lang=\"javascript\">\r\nvar getOptimizedRowSize = function (minItemsPerRow, maxItemsPerRow, itemCount) {\r\n\tvar highestRest = 0;\r\n\tvar highestRestItemsPerRow = 0;\r\n\r\n\tif (itemCount < maxItemsPerRow) {\r\n\t\treturn Math.max(minItemsPerRow, itemCount);\r\n\t}\r\n\r\n\tfor (var i = maxItemsPerRow; i >= minItemsPerRow; i = (i - 1) | 0) {\r\n\t\tvar rest = itemCount % i;\r\n\t\tif (rest === 0) {\r\n\t\t\treturn i;\r\n\t\t}\r\n\t\telse  {\r\n\t\t\tif (rest > highestRest) {\r\n\t\t\t\thighestRest = rest;\r\n\t\t\t\thighestRestItemsPerRow = i;\r\n\r\n\t\t\t\tif (highestRest >= ((maxItemsPerRow - 1) | 0) && itemCount > maxItemsPerRow) {\r\n\t\t\t\t\treturn i;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tif (highestRestItemsPerRow === 0) {\r\n\t\thighestRestItemsPerRow = maxItemsPerRow;\r\n\t}\r\n\r\n\treturn highestRestItemsPerRow;\r\n}\r\n\r\nvar getResultForArray = function(array) {\r\n\tvar count = array.length;\r\n\tvar maxItemsInOneRow = getOptimizedRowSize(3, 5, count);\r\n\tvar index = 0;\r\n\tvar row = 0;\r\n\r\n\tvar result = { count: count, itemsPerRow: maxItemsInOneRow, rows: []};\r\n\t\/\/console.log('total items', count);\r\n\t\/\/console.log('maxItemsInOneRow', maxItemsInOneRow);\r\n\twhile(index < count) {\r\n\t\tvar itemsLeft = count - index;\r\n\t\tvar itemsInRow = Math.min(itemsLeft, maxItemsInOneRow);\r\n\t\t\/\/console.log('row', row, 'item count', itemsInRow);\r\n\t\tresult.rows.push({ [row]: itemsInRow  });\r\n\t\tindex += itemsInRow;\r\n\t\trow += 1;\r\n\t}\r\n\tconsole.log(JSON.stringify(result));\r\n}\r\n\r\nvar array = [];\r\nfor(var i=1;i<30;i+=1) {\r\n\tarray.push(i);\r\n\tgetResultForArray(array);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>var getOptimizedRowSize = function (minItemsPerRow, maxItemsPerRow, itemCount) { var highestRest = 0; var highestRestItemsPerRow = 0; if (itemCount < maxItemsPerRow) { return Math.max(minItemsPerRow, itemCount); } for (var i = maxItemsPerRow; i >= minItemsPerRow; i = (i &#8211; 1) | 0) { var rest = itemCount % i; if (rest === 0) { return i; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/334\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}