Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycling an array on click

I have an array that has more than 12 elements, i.e.[ a,b,c,d,e,f,g,h,i,j,k,l]

In UI below this is how I want it to be displayed in a 3 X 3 grid.

a b c d e f g h <>

Where <> is a load more button

On each subsequent load more button click I need to display like

i j k l a b c d <>

and so on

e f g h i j k l <>

Basically I want to loop into the same array. At a time 8 elements will be shown along with <> button. I have the logic to display in grid ready, but load more I am having difficulty. Any guidance on how should I proceed?

like image 217
Mozak Avatar asked May 28 '26 16:05

Mozak


1 Answers

I think you need something like this:

var start = 0;
var arr = [ a,b,c,d,e,f,g,h,i,j,k,l];

function nextChunk(howMany) {
    var result = [];
    while (howMany--) {
        result.push(arr[start]);
        start = (start + 1) % arr.length;
    }
    return result;
}
like image 72
Lajos Arpad Avatar answered May 30 '26 07:05

Lajos Arpad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!