Swiper page --js turn two-dimensional array of one-dimensional arrays

When using Swiper display data, often they need to be paged, each page item to put a number on demand.

If we want to divide a multi-page display a school teacher, a teacher per page show n.

We get to the teacher list data is one-dimensional. If converted into two-dimensional array, it is easy to implement the presentation of two nested loops (e.g., v-for) by.

Wherein the first layer is responsible for generating a plurality of pages cycle (i.e. each page of a Swiper swiper-slide).

The second layer is responsible for generating a plurality of cycles responsible teacher page (one may have 1 ~ n).

 

The core is a one-dimensional array to a two-dimensional array, the code is implemented as follows:

The first parameter is one-dimensional array, the second parameter is the number per item to be displayed

function arrTo2d(arr, size) {
    let arr2d=[];
    for(let i=0;i<arr.length;i=i+size){
        arr2d.push(arr.slice(i,i+size));
    }
    return arr2d;
}

console.log(arrTo2d([1,2,3,4,5,6,7,8,9], 4))

Output Results: [[1, 2, 3, 4], [5, 6, 7, 8], [9]]

Guess you like

Origin www.cnblogs.com/flamestudio/p/12150945.html