Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to divide an array in blocks of 6 items max, however I can't find a better way to store the last block.

This is my current code and it works. Is there a way to avoid using the if after the .each ?

    var tabsData = [], tabCtr = 1, lpcCtr = 1;
    $.each(PAGE_DATA.tableData, function(i, item) {
            // show block of 6 items
            tabsData = [];
            lpcCtr = 0;
    if(tabsData.length > 0){
        // show last block
share|improve this question
up vote 3 down vote accepted

It's better to use slice function. It will also correctly process the last block

var size = PAGE_DATA.tableData.length;
for (var i = 0; i < size; i += 6){
     var t = PAGE_DATA.tableData.slice(i, i + 6);
     //show block of 6 items (or smaller in case of last block)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.