Shredding an array in a jade pattern

I have an array and mixin that takes an array as a parameter. How would I put an array in sections of four and move on to mixin?

So, something like this, but works better:

 each index, i in myArray
     if i%4 == 0
         +carouselItem([myArray[i], myArray[i+1], myArray[i+2], myArray[i+3]])
+3
source share
1 answer

I would suggest a slightly different approach: convert the array into a two-dimensional array into a request handler function, and then iterate over the two-dimensional array (array of arrays) in your Jade template.

With this approach, the template will be much simpler, and all conversions will occur inside the handler (controller) method, where you can have access to different libraries, etc.

Example: To convert an array to a two-dimensional array, you can use the following method:

function arrayTo2DArray (list, howMany) {
    var result = []; input = list.slice(0); 
    while(a[0]) { 
        result.push(a.splice(0, howMany)); 
    }
    return result;
}

:

exports.handler = function(req, res) {

    // myArray with some values

    res.render('template' { 
        myArray: arrayTo2DArray(myArray, 4) 
    }
}

myArray , , ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], arrayTo2DArray(myArray, 4)

[
    ['a', 'b', 'c', 'd'], 
    ['e', 'f', 'g', 'h']
]

Jade ( )

each values in myArray
    +carouselItem(values)

, .

+1

All Articles